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

发布于 2024-10-30 16:53:22 字数 556 浏览 1 评论 0原文

假设我有这个 JSON:

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

我将如何返回每条记录重复出现的键名称集?在本例中,为ID,标题

我尝试过:

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

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

没有成功。

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

Say I have this 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清秋悲枫 2024-11-06 16:53:22

这将为您提供一个包含在对象数组中匹配的所有字符串属性的数组。这就是您要找的吗?

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

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;
};
沐歌 2024-11-06 16:53:22

也许是这样的?

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

当然,这将返回任何一个对象中存在的项,而不是所有对象中都存在的项。从您的问题中尚不完全清楚这是否是您想要的解决方案。

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.

寻梦旅人 2024-11-06 16:53:22

这可能可以变得更加高效/简洁,但是下面的函数可以做到这一点。

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文