简体   繁体   English

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

[英]Get name of key in key/value pair in JSON using jQuery?

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, title

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. 这是一个JSON“数据库”,每个“记录”都有相同的密钥。 I just want a script that will tell me what the keys are, not test whether or not they occur in every entry. 我只想要一个能告诉我键是什么的脚本,而不是测试它们是否出现在每个条目中。

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

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. 如果这是您想要的解决方案,那么您的问题并不完全清楚。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM