获取json数据中所有key集合

时间:2022-06-08 19:16:58

 

/**
* 获取json数据中所有key
*/
function fetchResponseInfo(responseInfo){

//判断是否为空
if((!responseInfo || responseInfo.length <= 0)) {
return;
}

//获取model
var jsonCon = jQuery.parseJSON(responseInfo).model;
if(!jsonCon){
return;
}

//获取所有key
var keyArr;
if(jsonCon instanceof Array){
//1-JSONArray
if(jsonCon.length <= 0){
return;
}
var arrOb = jsonCon[0];
keyArr
= Object.keys(arrOb);
}
else if(jsonCon instanceof Object){
//2-JSONObject
keyArr = Object.keys(jsonCon);
}
else{
//3-Other
return;
}
if(!keyArr || keyArr.length <= 0){
return;
}

//组装数组
var temOb;
var lang = "java.lang.String";
var description = "";
var resArr = new Array();
for(i = 0; i < keyArr.length; i ++){
temOb
= new Object();
temOb.param
= keyArr[i];
temOb.lang
= lang;
temOb.description
= description;
resArr[i]
= temOb;
}
return resArr;

}