[英]Parse nested object in JSON string
I have this code:我有这个代码:
let test = '{"attribute_as":"plan_id","operator":"fromTo","values":"{"from":"70","to":"80"}"}';
console.log(JSON.parse(test));
It of course fail because in values
I have an object.它当然会失败,因为在
values
中我有一个 object。 Is there any option how to parse this string in easy way?有什么选择如何以简单的方式解析这个字符串? Or is it not possible at all?
还是根本不可能?
At the end the result should be:最后的结果应该是:
{
attribute_as: 'plan_id',
operator: 'fromTo',
values: {
from: 70,
to: 80
}
}
The string is incorrect:字符串不正确:
let err = '{"attribute_as":"plan_id","operator":"fromTo","values":"{"from":"70","to":"80"}"}'; // This is the original string let pass = '{"attribute_as":"plan_id","operator":"fromTo","values":{"from":70,"to":80}}'; // Corrected string let desiredObj = { attribute_as: 'plan_id', operator: 'fromTo', values: { from: 70, to: 80 } }; console.log(JSON.stringify(desiredObj) == err); console.log(JSON.stringify(desiredObj) == pass);
This should do the trick.这应该可以解决问题。 When logged both evaluated correctly.
记录时都正确评估。
let
test = '{"attribute_as": "plan_id","operator": "fromTo","values": {"from": 70,"to": 80}}',
test2 = {
attribute_as: 'plan_id',
operator: 'fromTo',
values: {
from: 70,
to: 80
}
}
console.log(JSON.parse(test));
console.log(JSON.stringify(test2));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.