如何在JavaScript中解析JSON数组?

时间:2014-12-19 07:06:28

标签: javascript json

我有像下面这样的JSON编码数据,并希望解析为JSON对象,以便将每个记录放在一个单独的数组中。比如string[] name={"Prakash","Pruthvi","Pronnoy"} ...

{"users":[{"name":"Prakash","phone":"1234","address":"Bangalore"},
{"name":"Pruthvi","phone":"3456","address":"Chennai"},
{"name":"Pronnoy","phone":"2345","address":"Mumbai"}]}

3 个答案:

答案 0 :(得分:1)

您实际上可以通过几次Google点击完成此操作。

无论如何,这就是你想要的:

       var str = JSON.stringify(YOURJSON);
       /* Converts to json notation */
       var data = JSON.parse(str);
       /* Converts to json array */           
       var i =0;
       /* loop through your data */
       while(data.users.length > 0) { 
         if(data.users[i] !== null && data.users[i] !== undefined )
            /* Last object always returns null */
            nameArr.push(data.users[i].name);
         i++; 
       }

希望这有帮助!

答案 1 :(得分:0)

答案 2 :(得分:0)

试试这个

<script>
var arr = {"users":[
{"name":"Prakash","phone":"1234","address":"Bangalore"},
{"name":"Pruthvi","phone":"3456","address":"Chennai"},
{"name":"Pronnoy","phone":"2345","address":"Mumbai"}
]};
var name1 = new Array(100);
var phone1 = new Array(100);
var add1 = new Array(100);
for (var i = 0; i < arr.users.length; i++) {
    var counter = arr.users[i];
    name1.push(counter.name);
    phone1.push(counter.phone);
    add1.push(counter.address);
}
</script>

使用name1,phone1,add1进一步使用。