JSON取值问题
发布于 8 年前 作者 ladigue 5046 次浏览 来自 问答
{
"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].urlJSON.parse(xxx.attUrl[0]).url 都取不到:S

6 回复

错了, 是JSON.parse(allJsonText).attUrl[0].url

来自炫酷的 CNodeMD

@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

重新组织JSON格式,将 "{“url”:"http://xxxx/1"}", “{“url”:“http://xxxx/2”}” 两边的双引号去掉。

{
"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对象本身结构有问题,如上即可

回到顶部