如何循环json数组以获取javascript中的键和值?
我有下面的json数组结构。。如何获取每个如何循环json数组以获取javascript中的键和值?,javascript,json,Javascript,Json,我有下面的json数组结构。。如何获取每个记录json对象的键和值 { "records": [{ "cfsub_2": "1", "cf_7": "1/3/2016", "cf_1": "Clinic San", "cf_2": "Fever", "cf_3": "56.60", "cfe_8": "dsf4334" }, { "cfsub_2": "2",
记录
json对象的键和值
{
"records": [{
"cfsub_2": "1",
"cf_7": "1/3/2016",
"cf_1": "Clinic San",
"cf_2": "Fever",
"cf_3": "56.60",
"cfe_8": "dsf4334"
}, {
"cfsub_2": "2",
"cf_7": "3/3/2016",
"cf_1": "Clinic Raju",
"cf_2": "braces",
"cf_3": "183.50",
"cfe_8": "fresr4"
}]
}
我的预期输出是获取密钥和值。。。以下为示例:
<b>key</b> : cf_1, <b>value</b> : Clinic San
完整的JSON结构如下所示:
{
"forms": [{
"id": 1,
"records": [{
"cfsub_2": "1",
"cf_7": "1/3/2016",
"cf_1": "Clinic San",
"cf_2": "Fever",
"cf_3": "56.60",
"cfe_8": "dsf4334"
}, {
"cfsub_2": "2",
"cf_7": "3/3/2016",
"cf_1": "Clinic Raju",
"cf_2": "braces",
"cf_3": "183.50",
"cfe_8": "fresr4"
}]
}, {
"id": 7,
"records": [{
"cf_31": "27/3/2016",
"cf_32": "Singapore",
"cf_33": "dfd555",
"cfe_34": ""
}]
}, {
"id": 11,
"records": [{
"cfsub_10": "9",
"cf_9": "25/3/2016",
"cf_10": "256.50",
"cfe_11": "dfg44"
}]
}]
}
var getKeys=函数(arr){
变量键,键=[];
对于(i=0;i
希望这一条对您有所帮助
$.each(value.forms, function(index,array){
$.each(array.records, function(ind,items){
$.each(items, function(indo,itemso){
alert( "Key -> "+indo + " : values -> " + itemso );
});
});
});
请澄清您想要什么,您目前拥有什么,预期的输出是什么,以及您已经尝试解决了什么问题。
var getKeys = function (arr) {
var key, keys = [];
for (i = 0; i < arr.length; i++) {
for (key in arr[i]) {
if (arr[i].hasOwnProperty(key)) {
keys.push(key);
}
}
}
return keys;
};
$.each(value.forms, function(index,array){
$.each(array.records, function(ind,items){
$.each(items, function(indo,itemso){
alert( "Key -> "+indo + " : values -> " + itemso );
});
});
});