Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jquery为json字符串中的关联数组获取索引?_Javascript_Jquery_Json - Fatal编程技术网

Javascript 如何使用jquery为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"

我有一个json字符串,如下所示:

{
"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
   });
 });