简体   繁体   English

将类似数组的字符串解析为JavaScript中的json对象数组

[英]Parse array-like string into an array of json objects in JavaScript

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. 它类似于两个json对象的数组。 I need to retrieve these JSON objects. 我需要检索这些JSON对象。

I have tried to parse it using JSON.parse(str) but complaints about the "," that separates JSONs in this array-like structure. 我曾尝试使用JSON.parse(str)解析它,但抱怨“,”在这种类似数组的结构中分离了JSON。

I have tried doing str.split(",") but it splits by each comma, eg 我试过做str.split(“,”),但它按每个逗号分割,例如

  [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. 非常感谢您提供有关如何将该字符串读取到JSON对象数组中的任何帮助。

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"}]}]'); 这对我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"}]}]'); (没有JSON解析错误)> 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 在Chrome控制台中尝试过

尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM