1

我有一个外部文件contacts.json。如何将其转换为 javascript 数组?
这是contacts.json内容:

{
    "ppl1":{
        "Name":"Jhon",
        "Surname":"Kenneth",
        "mobile":329129293,
        "email":"jhon@gmail.com"
    },
    "ppl2":{
        "Name":"Thor",
        "Surname":"zvalk",
        "mobile":349229293,
        "email":"thor@gmail.com"
    },
    "ppl3":{
        "Name":"Mila",
        "Surname":"Kvuls",
        "mobile":329121293,
        "email":"mila@gmail.com"
    }
}
4

3 回答 3

12

解决了:

$.getJSON('contacts.json', function (json) {
var array = [];
for (var key in json) {
    if (json.hasOwnProperty(key)) {
        var item = json[key];
        array.push({
            name: item.Name,
            surname: item.Surname,
            mobile: item.mobile,
            email: item.email
        });            
    }
}
});
于 2013-01-31T11:10:36.677 回答
1
var items = [];
$.each(JSONObject.results.bindings, function(i, obj) {
    items.push([obj.place.value, obj.lat.value, obj.long.value, obj.page.value]);
});
于 2013-01-26T18:44:18.607 回答
-1

在这里回答。

// JavaScript array of JavaScript objects
var objs = json_string.map(JSON.parse);

// ...or for older browsers
var objs=[];
for (var i=json_string.map.length;i--;) objs[i]=JSON.parse(json_string.map[i]);

// ...or for maximum speed:
var objs = JSON.parse('['+json_string.map.join(',')+']');
于 2013-01-26T18:44:12.273 回答