我想要这个名字来自Salem,madurai来自这个json
{
"status": "success",
"DisplayList": [
{
"AVINASHI": [
"gmail@com",
"gmail@hp.com"
]
},
{
"AVINASHI": [
"gmail@com",
"gmail@hp.com"
]
},
{
"ERNAVOOR": [
"sri@gmail.com",
"sri@gmail.com"
]
},
{
"ERNAVOOR": [
"sri@gmail.com",
"sri@gmail.com"
]
},
{
"HYDCURD": [
"sri@gmail.com"
]
},
{
"KANCHIPURAM": [
"sri@gmail.com"
]
},
{
"KEELKATTLAI": [
"sri@gmail.com"
]
},
{
"MADURAI": [
"sri@gmail.com",
"sri2@gmail.com"
]
},
{
"MADURAI": [
"sri@gmail.com",
"sri2@gmail.com"
]
},
{
"SALEM": [
"gmail@hp.com",
"gmail@hp.com",
"gmail@hp1.com"
]
},
{
"SALEM": [
"gmail@hp.com",
"gmail@hp.com",
"gmail@hp1.com"
]
},
{
"SALEM": [
"gmail@hp.com",
"gmail@hp.com",
"gmail@hp1.com"
]
}
]
}
答案 0 :(得分:3)
var List = JSONObject['DisplayList'];
for(var i=0;i<List.length;i++)
{
for(var key in List[i])
{
console.log(key); //will print names
console.log(List[i][key]);//will print array of email
}
}
假设您拥有变量JSONObject
答案 1 :(得分:2)
var obj = { status:'success', DisplayList: [{...}, {...}]} // your object
var l = obj.DisplayList.length,
result = [];
while (l--){
result = result.concat(Object.keys(obj.DisplayList[l]));
}
console.log(result.join(", "));
此方法使用Object.keys
来枚举对象属性,而不包括原型属性。它只是IE9 +,所以我们仍然需要一个插件:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Compatibility
答案 2 :(得分:0)
尝试
for(var i in json.DisplayList) //Gets Array
for(var k in json.DisplayList[i]) // Get object on 'i'th index of the DisplayList Array
console.log(k); //Get the key of that object.
答案 3 :(得分:0)
以下是代码:
var DisplayList = JSONObject['DisplayList'];
var NamesList = [];
for(var i=0; i<DisplayList.length; i++)
{
for(var keyName in List[i])
{
if(NamesList.indexOf(keyName) >= 0)
{
NamesList.push(keyName);
}
}
}
console.log(NamesList); //Output: ['AVINASHI','ERNAVOOR','HYDCURD',...]
//Not the names will not repeat.. the array will have unique values