鑑於JSON是這樣的:獲取對象
{ "rss": {
"page": 1,
"results": [{
"type": "text",
"$": 10
}],
"text": [{
"content": "Lorem ipsum dolor sit amet.",
"author": {
"name": "Cesar",
"email": "[email protected]"
},
},
{
"content": "Tema Tis rolod muspi merol.",
"author": {
"name": "Cleopatre",
"email": "[email protected]"
},
}]
}
在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"}
]
試試'VAL [「作者」] [「名稱」]' – 2012-07-23 14:50:36
好,這可能會奏效,但在這種情況下,我需要存儲兩個字符串:「作家」,那麼「名」,當我想存儲只有一個代表authorname,如xpath或css路徑... – enguerran 2012-07-23 14:52:11
快速和髒var name = eval('return val。'+ key +';'); – yent 2012-07-23 14:52:49