简体   繁体   中英

How to convert JSON Array to Javascript Object Array

I have the following JSON value pushed from the server.

         result=[{"id":1492,"name":"Delhi"},
                 {"id":109,"name":"Coimbatore"},
                 {"id":576,"name":"Konni"},
                 {"id":525,"name":"Kottayam"}
                ]

I know how to convert JSON Array to Javascript Array.Here is the code below(got from stackoverflow)

        var locations = [];
        $.each(result, function(i, obj) {
            locations.push([obj.id,obj.name]);
        });

I want to convert this JSON Array to a JavaScript Object Array so that I can access the values as jarray[0].id which will give me the value 1492. Please advice

You don't need to do anything to the result array. Just use result[0].id and it will evaluate to 1492 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM