给定这样的json:
{ "rss": {
"page": 1,
"results": [{
"type": "text",
"$": 10
}],
"text": [{
"content": "Lorem ipsum dolor sit amet.",
"author": {
"name": "Cesar",
"email": "cesar@evoria.com"
},
},
{
"content": "Tema Tis rolod muspi merol.",
"author": {
"name": "Cleopatre",
"email": "cleopatre@pyramid.com"
},
}]
}
在 javascript 中,我可以像这样检索值:
var json = JSON.parse(datajson);
$.each(json.text, function(key, val) {
// this one is ok
var content = val['content'];
// this one does not work
var authorname = val['author.name'];
});
这是一种方式,给定字符串格式的属性名称,检索复杂对象的值,例如json.text[0].author.name
?
编辑 我想将所需的属性存储在另一个对象中,例如:
[
{ dt: "Text content", dd: "content" },
{ dt: "Author name", dd: "author.name"}
]