[英]How to parse JSON string within a JSON object
There is a JSON string in a JSON object JSON对象中有一个JSON字符串
{
"abc": "{\n \"_count\": 10,\n \"_start\": 0,\n \"_total\": 60\n }",
"success": true
}
I want to get the the the value of abc
as JSON object in javascript. 我想在javascript中将
abc
的值作为JSON对象获取。
You would use something like this: 您将使用以下内容:
var obj = JSON.parse(JSON.parse(the_string).abc);
NOTE: Your JSON is invalid. 注意:您的JSON无效。 Please correct it.
请更正。 It should be somewhat like the below:
它应该类似于以下内容:
{
"abc": "{\n \"_count\": 10,\n \"_start\": 0,\n \"_total\": 60\n}",
"success": true
}
If your object is in a variable called obj
then obj.abc
will return the string value. 如果您的对象位于名为
obj
的变量中,则obj.abc
将返回字符串值。 As this is a JSON string encoding a JavaScript object you need to use JSON.parse to convert it : var abc = JSON.parse (obj.abc);
由于这是一个编码JavaScript对象的JSON字符串,因此您需要使用JSON.parse进行转换:
var abc = JSON.parse (obj.abc);
. 。 You now have access to the field vaues
abc._count
, abc._start
and abc._total
. 现在,您可以访问字段vaues
abc._count
, abc._start
和abc._total
。
You can do something like this 你可以做这样的事情
var json = '{"abc": {"_count": 10,"_start": 0, "_total": 60 },"success": true}';
var obj = JSON.parse(json);
console.log(obj.success);
console.log(obj.abc['_count']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.