0

I have a String in the following format:

[{"type":"relativeHumidity","unit":"%","resolution":300,"accuracy":0.0,"period":"INSTANT","correction":false,"completenessRatios":[{"completenessRatio":0.0,"period":"SIX_MONTHS"},{"completenessRatio":0.0,"period":"THIRTY_DAYS"},{"completenessRatio":0.5789037386601943,"period":"FULL_HISTORY"}]},{"type":"temperature","unit":"C","resolution":300,"accuracy":0.0,"period":"INSTANT","correction":false,"completenessRatios":[{"completenessRatio":0.0,"period":"SIX_MONTHS"},{"completenessRatio":0.0,"period":"THIRTY_DAYS"},{"completenessRatio":0.5789037386601943,"period":"FULL_HISTORY"}]}]

It resembles an array of two json objects. I need to retrieve these JSON objects.

I have tried to parse it using JSON.parse(str) but complaints about the "," that separates JSONs in this array-like structure.

I have tried doing str.split(",") but it splits by each comma, e.g.

  [0] [{"type":"relativeHumidity"

  [1] "unit":"%"

I have tried removing first and last brackets and reading it as an array:

var arr = [str.substr(1, value.length - 2)];

[ '{"type":"relativeHumidity","unit":"%","resolution":300,"accuracy":0.0,"period":"INSTANT","correction":false,"completenessRatios":[{"completenessRatio":0.0,"period":"SIX_MONTHS"},{"completenessRatio":0.0,"period":"THIRTY_DAYS"},{"completenessRatio":0.5789037386601943,"period":"FULL_HISTORY"}]},{"type":"temperature","unit":"C","resolution":300,"accuracy":0.0,"period":"INSTANT","correction":false,"completenessRatios":[{"completenessRatio":0.0,"period":"SIX_MONTHS"},{"completenessRatio":0.0,"period":"THIRTY_DAYS"},{"completenessRatio":0.5789037386601943,"period":"FULL_HISTORY"}]}' ]

but it creates an array of a single string.

Any help on how to read that string into an array of JSON objects will be greatly appreciated.

1
  • array[0], array[1] should give you the two json strings. Commented Nov 25, 2013 at 14:09

2 Answers 2

1

this works for me (no JSON parse error) > JSON.parse('[{"type":"relativeHumidity","unit":"%","resolution":300,"accuracy":0.0,"period":"INSTANT","correction":false,"completenessRatios":[{"completenessRatio":0.0,"period":"SIX_MONTHS"},{"completenessRatio":0.0,"period":"THIRTY_DAYS"},{"completenessRatio":0.5789037386601943,"period":"FULL_HISTORY"}]},{"type":"temperature","unit":"C","resolution":300,"accuracy":0.0,"period":"INSTANT","correction":false,"completenessRatios":[{"completenessRatio":0.0,"period":"SIX_MONTHS"},{"completenessRatio":0.0,"period":"THIRTY_DAYS"},{"completenessRatio":0.5789037386601943,"period":"FULL_HISTORY"}]}]');

tried it in chrome console

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I was unnecessarily removing first and last bracket, of which I forgot to mention in the post. It works perfectly now.
0

try this:

var jsonArray = JSON.stringify({ "type":"relativeHumidity","unit":"%","resolution":300,"accuracy":0.0;})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.