Javascript 如何使用jquery为json字符串中的关联数组获取索引?
我有一个json字符串,如下所示:Javascript 如何使用jquery为json字符串中的关联数组获取索引?,javascript,jquery,json,Javascript,Jquery,Json,我有一个json字符串,如下所示: { "2045532196113651": [{ "height": 42, "width": 75, "source": "https://url1" }, { "height": 42, "width": 75, "source": "https://url2" }], "2045532296113641": [{ "height": 50, "width": 75, "source"
{
"2045532196113651": [{
"height": 42,
"width": 75,
"source": "https://url1"
}, {
"height": 42,
"width": 75,
"source": "https://url2"
}],
"2045532296113641": [{
"height": 50,
"width": 75,
"source": "https://url3"
}, {
"height": 50,
"width": 75,
"source": "https://url4"
}]
}
该字符串具有变量名jsondata
在本例中,我想获得json对象(2045532196113651和20455322966113641)中的索引数。我还想获得4个URL(url1、url2、url3和url4)
我该怎么做?我可以使用jquery。首先解析字符串以便获得一个对象,然后可以循环它:
var obj = $.parseJSON(jsondata);
$.each(obj, function(key, value) {
// key is the name of the item, e.g. "2045532196113651"
// value is an array:
$.each(value, function(idx, item) {
// item.source contains the URL
});
});