开发者

How to get indexes for associative array in json string using jquery?

I have a string that is json like this:

{
"204553219611开发者_如何学Go3651": [{
    "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"
}]

}

The string has the variable name jsondata.

I want to get the numbers of the indexes in the json object (2045532196113651 and 2045532296113641) in this example. Also I want to get the 4 urls (url1, url2, url3, and url4).

How do I do that? I can use jquery.


First you parse the string so that you get an object, then you can loop it:

 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
   });
 });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜