0

我想要这个 json 中的像 salem,madurai 这样的名字

{
    "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"
            ]
        }
    ]
}
4

4 回答 4

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
    }
}

假设您在变量中有 JSON 数据JSONObject

于 2013-06-21T05:25:33.280 回答
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.keyswhich 旨在枚举对象属性而不包括原型属性。它只是 IE9+,所以我们仍然需要一个插件:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Compatibility

于 2013-06-21T05:27:25.980 回答
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.
于 2013-06-21T05:27:19.793 回答
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
于 2013-06-21T06:08:37.280 回答