JSON取值问题
{
"id": "5631fbacfb26458c1a48e465",
"user_id": "562f773bfb26458c1a48e45f",
"content": "这是一个测试内容",
"attUrl": [
"{"url":"http://xxxx/1"}",
“{"url":"http://xxxx/2"}”
],
"replydate": "2015-10-29T10:57:48.228Z",
"createdate": “2015-10-29T10:57:48.228Z”
}
以上JSON中如何取到字符串 http://xxxx/1
我用 attUrl[0].url
和 JSON.parse(xxx.attUrl[0]).url
都取不到:S
6 回复
@klesh 非常感谢,不过这样依然无法取到。本身不用JSON.parse也可以用allJsonText.attUrl[0]取到{“url”:"http://xxxx/1"},但就是无法取到url值。对了,我是在html中直接用{{}}取值的。
你这个JSON格式有点问题吧
var a = {
"id": "5631fbacfb26458c1a48e465",
"user_id": "562f773bfb26458c1a48e45f",
"content": "这是一个测试内容",
"attUrl": [
"{\"url\":\"http://xxxx/1\"}",
"{\"url\":\"http://xxxx/2\"}"
],
"replydate": "2015-10-29T10:57:48.228Z",
"createdate": "2015 - 10 - 29 T10: 57: 48.228 Z"
}
console.log(a)
console.log(JSON.parse(a.attUrl[0]).url) // http://xxxx/1
{
"id": "5631fbacfb26458c1a48e465",
"user_id": "562f773bfb26458c1a48e45f",
"content": "这是一个测试内容",
"attUrl": [
’{"url":"http://xxxx/1"}‘,
‘{"url":"http://xxxx/2"}’
],
"replydate": "2015-10-29T10:57:48.228Z",
"createdate": “2015-10-29T10:57:48.228Z”
}
试试这个?
{ “id”: “5631fbacfb26458c1a48e465”, “user_id”: “562f773bfb26458c1a48e45f”, “content”: “这是一个测试内容”, “attUrl”: [ { “url”: "http://xxxx/1" }, { “url”: "http://xxxx/2" } ], “replydate”: “2015-10-29T10:57:48.228Z”, “createdate”: “2015-10-29T10: 57: 48.228Z” } 给出的JSON对象本身结构有问题,如上即可