使用jQuery获取JSON中键/值对中键的名称?

时间:2022-10-26 13:30:56

Say I have this JSON:

说我有这个JSON:

[
    {
        "ID": "1",
        "title": "Title 1",
    },
    {
        "ID": "2",
        "title": "Title 2",
    }
]

How would I return the set of key names that recur for each record? In this case, ID, title.

如何返回每条记录重复出现的一组密钥名称?在这种情况下,ID,标题。

I tried:

$.getJSON('testing.json', function(data) {
  var items = [];
  $.each(data, function(key, val) {
    items.push(key +', ');
  });

  $('<p/>', {
     html: items.join('')
  }).appendTo('#content');
});

without success.

This is a JSON "database", and every "record" has the same keys. I just want a script that will tell me what the keys are, not test whether or not they occur in every entry.

这是一个JSON“数据库”,每个“记录”都有相同的键。我只想要一个能告诉我键是什么的脚本,而不是测试它们是否出现在每个条目中。

3 个解决方案

#1


5  

This will give you an array of all the string properties that match across an array of objects. Is that what you are looking for?

这将为您提供一个数组,其中包含与对象数组匹配的所有字符串属性。这是你在找什么?

$.getJSON('testing.json', function(data) {
    var propertiesThatExistInAll = getPropertiesThatExistInAll(data);
});


var getPropertiesThatExistInAll = function(arr) {
    var properties = $.map(data[0], function (prop, value) {
        return prop;
    });

    var propertiesThatExistInAll = [];

    $.each(properties, function (index, property) {
        var keyExistsInAll = true;

        // skip the first one since we know it has all the properties
        for (var i = 1, len = data.length; i < len; i++) {
            if (!data[i].hasOwnProperty(property)) {
                keyExistsInAll = false;
                break;
            }
        }

        if (keyExistsInAll) {
            propertiesThatExistInAll.push(property);
        }
    });

    return propertiesThatExistInAll;
};

#2


1  

Something like this, perhaps?

也许是这样的事情?

items = [];
for (key in jsonobj) {
    if (!itemExists(items, key)) {
        items[items.length] = key
    }
}

function itemExists(items, value) {
    for (i = 0; i < items.length; i++) {
        if (items[i] == value) {
            return true
        }
    }
    return false;
}

Of course, that will return items that exist in any one of the objects, not that exist in all. It's not entirely clear from your question if this is the solution you want.

当然,这将返回任何一个对象中存在的项目,而不是所有对象中存在的项目。如果这是您想要的解决方案,那么您的问题并不完全清楚。

#3


1  

This can probably be made more efficient/concise, but the function below will do it.

这可能会更有效/更简洁,但下面的功能将做到这一点。

var testJson = [ {'oi' : 1, 'arf': 2, 'foo' : 0}, {'oi': 5, 'arf': 7}];

function commonKeys(j)
{

    var fillUp = [];
    for(var i in j[0])
       fillUp.push(i);

    for(var i = 1; i < j.length; i++)
    {
       var cur = j[i]; var curArr = [];
       for (var i in cur) {curArr.push(i)};
       fillUp = fillUp.filter(function(x) {return (curArr.indexOf(x) != -1);});
    }

    return fillUp;
}

alert(commonKeys(testJson)); //oi,arf (not foo)

#1


5  

This will give you an array of all the string properties that match across an array of objects. Is that what you are looking for?

这将为您提供一个数组,其中包含与对象数组匹配的所有字符串属性。这是你在找什么?

$.getJSON('testing.json', function(data) {
    var propertiesThatExistInAll = getPropertiesThatExistInAll(data);
});


var getPropertiesThatExistInAll = function(arr) {
    var properties = $.map(data[0], function (prop, value) {
        return prop;
    });

    var propertiesThatExistInAll = [];

    $.each(properties, function (index, property) {
        var keyExistsInAll = true;

        // skip the first one since we know it has all the properties
        for (var i = 1, len = data.length; i < len; i++) {
            if (!data[i].hasOwnProperty(property)) {
                keyExistsInAll = false;
                break;
            }
        }

        if (keyExistsInAll) {
            propertiesThatExistInAll.push(property);
        }
    });

    return propertiesThatExistInAll;
};

#2


1  

Something like this, perhaps?

也许是这样的事情?

items = [];
for (key in jsonobj) {
    if (!itemExists(items, key)) {
        items[items.length] = key
    }
}

function itemExists(items, value) {
    for (i = 0; i < items.length; i++) {
        if (items[i] == value) {
            return true
        }
    }
    return false;
}

Of course, that will return items that exist in any one of the objects, not that exist in all. It's not entirely clear from your question if this is the solution you want.

当然,这将返回任何一个对象中存在的项目,而不是所有对象中存在的项目。如果这是您想要的解决方案,那么您的问题并不完全清楚。

#3


1  

This can probably be made more efficient/concise, but the function below will do it.

这可能会更有效/更简洁,但下面的功能将做到这一点。

var testJson = [ {'oi' : 1, 'arf': 2, 'foo' : 0}, {'oi': 5, 'arf': 7}];

function commonKeys(j)
{

    var fillUp = [];
    for(var i in j[0])
       fillUp.push(i);

    for(var i = 1; i < j.length; i++)
    {
       var cur = j[i]; var curArr = [];
       for (var i in cur) {curArr.push(i)};
       fillUp = fillUp.filter(function(x) {return (curArr.indexOf(x) != -1);});
    }

    return fillUp;
}

alert(commonKeys(testJson)); //oi,arf (not foo)