这里我将JSON加载到变量中。但我无法找到嵌套JSON的长度。
var JVarible = [{"key":{"kind":"Comment","id":5992578889547776},"categoryId":0,"userName":"Shana Deepak","userId":"cpshana","comment":"hi.fghfghfgh ","createDate":"Sep 16, 2013 7:07:36 AM","url":"https://graph.facebook.com/100000840303512/picture?type\u003dsmall","networkType":"facebook","status":1,"nestmsgs":{"value":"[{\"key\":{\"kind\":\"Nestmsg\",\"id\":5914238686068736},\"commentId\":5992578889547776,\"userName\":\"Shana Deepak\",\"userId\":\"cpshana\",\"message\":\"dfgdfgfdg\",\"createDate\":\"Sep 16, 2013 7:22:01 AM\",\"url\":\"https://graph.facebook.com/100000840303512/picture?type\\u003dsmall\",\"networkType\":\"facebook\",\"status\":0},{\"key\":{\"kind\":\"Nestmsg\",\"id\":5281469744283648},\"commentId\":5992578889547776,\"userName\":\"Shana Deepak\",\"userId\":\"cpshana\",\"message\":\"gfdgdfgfd\",\"createDate\":\"Sep 16, 2013 7:12:25 AM\",\"url\":\"https://graph.facebook.com/100000840303512/picture?type\\u003dsmall\",\"networkType\":\"facebook\",\"status\":0}]"}}];
var i=0;
for (i=0; i<JVarible.length;i++)
{
alert(JVarible[i].['nestmsgs'].length)
}
答案 0 :(得分:2)
首先,JVarible
不包含JSON。它包含一个JavaScript 数组。但是该数组中的一个值确实是JSON(包含在字符串中)。
你的问题不是很清楚,但似乎你想获得每个对象中的消息数量。 nestmsgs
实际上是一个具有一个属性value
的对象。 value
有一个包含JSON作为值的字符串。
首先必须解析JSON,这会产生一个数组,然后你可以确定它的长度:
for (var i = 0; i < JVarible.length; i++) {
var msgobj = JVarible[i].nestmsgs;
msgobj.value = JSON.parse(msgobj.value);
alert(msgobj.value.length)
}
答案 1 :(得分:1)
这样的东西?
Object.keys(JVarible[0]) //returns ["key", "categoryId", "userName", "userId", "comment", "createDate", "url", "networkType", "status", "nestmsgs"]
Object.keys(JVarible[0]).length //returns 10