-3

是的,有许多线程质疑与以下类似的问题,但我发现关于动态键和从每个键保存多个值的 jsons 中提取单个值的帮助非常少。

我有一个 json,其中的键是动态的,我需要能够调用每个单独的值。

有任何想法吗?

json 示例如下:

{"AppliedPrepaidBundle":{"id":["14","15","24","25","26","27","28","29","30","31"],"prepaid_bundle_id":["5","5","5","5","5","5","5","5","5","5"]},"Device":{"id":["77","77","91","91","117","117","117","117","117","124"]}}

我玩过以下代码,但目前只能吐出一串值而不是单个值:

        $.each(data, function (key1, value1) {
            $.each(value1, function (key, value) {
                $('body').append('<li id="' + key + '">' + key1 +' ' + key +' ' + value + '</li>');
            });
        });

解决了这个问题:

        json = JSON.parse(data);
        for (var index in json) {
            $.each(json[index], function(key,value) {
                for(var i = 0; i< json[index][key].length; i++){
                    $('body').append('<li>' + index +' ' + key +' ' + json[index][key][i] + '</li>');
                }
            });
        }
4

4 回答 4

0

JSON 在 JavaScript 中返回一个对象。所以你可以做这样的事情:

var json = {"AppliedPrepaidBundle":{"id":["14","15","24","25","26","27","28","29","30","31"],"prepaid_bundle_id":["5","5","5","5","5","5","5","5","5","5"]},"Device":{"id":["77","77","91","91","117","117","117","117","117","124"]}};

for (var i=0; i<json.AppliedPrepaidBundle.id.length; i++) {
    console.log("id"+i+": "+json.AppliedPrepaidBundle.id[i]);
}

这将打印出 ID 对象的所有值:14、15、24、25 等

于 2013-10-25T08:32:28.817 回答
0

从此示例中,您可以访问所有元素

var json = {"AppliedPrepaidBundle":{"id":["14","15","24","25","26","27","28","29","30","31"],"prepaid_bundle_id":["5","5","5","5","5","5","5","5","5","5"]},"Device":{"id":["77","77","91","91","117","117","117","117","117","124"]}};

for (var i=0; i<json.AppliedPrepaidBundle.id.length; i++) {
   $('body').append("<li>AppliedPrepaidBundle id"+i+": "+json.AppliedPrepaidBundle.id[i]+'</li>');
}
for (var i=0; i<json.AppliedPrepaidBundle.prepaid_bundle_id.length; i++) {
    $('body').append("<li>PrepaidBundleid"+i+":"+json.AppliedPrepaidBundle.prepaid_bundle_id[i]+'</li>');
}
for (var i=0; i<json.Device.id.length; i++) {
    $('body').append("<li>Device id"+i+": "+json.Device.id[i]+'</li>');

}

这是小提琴

于 2013-10-25T08:35:59.503 回答
0

用于JSON.parse创建对象。(请参阅如何在 JavaScript 中解析 JSON)。然后使用 循环遍历属性for (x in yourObject) { ... }

var jsonObject = JSON.parse('your JSON-String');
for (property in jsonObject) {
    // do something with jsonObject[property]
    console.log(property + ' ' + jsonObject[property]);
}
于 2013-10-25T08:36:40.847 回答
0

您可以使用 javascript 基本功能:http: //jsfiddle.net/taUng/

var data = {"AppliedPrepaidBundle":{"id":["14","15","24","25","26","27","28","29","30","31"],"prepaid_bundle_id":["5","5","5","5","5","5","5","5","5","5"]},"Device":{"id":["77","77","91","91","117","117","117","117","117","124"]}};
for (var dataIndex in data) {
     console.log(dataIndex, data[dataIndex]);
     var subData = data[dataIndex];
     for (var subDataIndex in subData) {
         console.log(subDataIndex, subData[subDataIndex]);
     }
}

等等...

当您适合 javascript 时,您还可以使用 Recursion 来避免重复自己。(http://en.wikipedia.org/wiki/Dont_repeat_yourself

于 2013-10-25T08:38:10.510 回答