是的,有许多线程质疑与以下类似的问题,但我发现关于动态键和从每个键保存多个值的 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>');
}
});
}