Javascript 使用 jQuery 循环并获取 JSON 数组的键/值对

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7861032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:52:21  来源:igfitidea点击:

Loop and get key/value pair for JSON array using jQuery

javascriptjqueryjson

提问by JStark

I'm looking to loop through a JSON array and display the key and value.

我希望遍历 JSON 数组并显示键和值。

It should be a simplified version of the following post, but I don't seem to have the syntax correct: jQuery 'each' loop with JSON array

它应该是以下帖子的简化版本,但我的语法似乎不正确:jQuery 'each' loop with JSON array

I also saw the post Get name of key in key/value pair in JSON using jQuery?, but it also seemed like lots of code for a simple activity.

我还看到了使用 jQuery 在 JSON 中获取键/值对中键的名称的帖子,但对于一个简单的活动来说,它似乎也有很多代码。

This illustrates what I'm looking for (but it doesn't work):

这说明了我正在寻找的东西(但它不起作用):

var result = '{"FirstName":"John","LastName":"Doe","Email":"[email protected]","Phone":"123 dead drive"}';
$.each(result, function(k, v) {
             //display the key and value pair
            alert(k + ' is ' + v);
        });

There is no mandatory jQuery requirement, but it is available. I can also restructure the JSON if it cuts down the required code.

没有强制性的 jQuery 要求,但它是可用的。如果它减少了所需的代码,我也可以重构 JSON。

回答by Darin Dimitrov

You have a string representing a JSON serialized JavaScript object. You need to deserialize it back to a JavaScript object before being able to loop through its properties. Otherwise you will be looping through each individual character of this string.

您有一个表示 JSON 序列化 JavaScript 对象的字符串。您需要将其反序列化回 JavaScript 对象,然后才能循环遍历其属性。否则,您将遍历此字符串的每个单独字符。

var resultJSON = '{"FirstName":"John","LastName":"Doe","Email":"[email protected]","Phone":"123 dead drive"}';
var result = $.parseJSON(resultJSON);
$.each(result, function(k, v) {
    //display the key and value pair
    alert(k + ' is ' + v);
});

Live demo.

现场演示

回答by xdazz

var obj = $.parseJSON(result);
for (var prop in obj) {
    alert(prop + " is " + obj[prop]);
}

回答by Oskar

You can get the values directly in case of one array like this:

如果是这样的一个数组,您可以直接获取值:

var resultJSON = '{"FirstName":"John","LastName":"Doe","Email":"[email protected]","Phone":"123 dead drive"}';
var result = $.parseJSON(resultJSON);
result['FirstName']; // return 'John'
result['LastName'];  // return ''Doe'
result['Email']; // return '[email protected]'
result['Phone'];  // return '123'

回答by Smith

The following should work for a JSON returned string. It will also work for an associative array of data.

以下应该适用于 JSON 返回的字符串。它也适用于关联的数据数组。

for (var key in data)
     alert(key + ' is ' + data[key]);

回答by mythicalcoder

Parse the JSON string and you can loop through the keys.

解析 JSON 字符串,您可以遍历键。

var resultJSON = '{"FirstName":"John","LastName":"Doe","Email":"[email protected]","Phone":"123 dead drive"}';
var data = JSON.parse(resultJSON);

for (var key in data)
{
    //console.log(key + ' : ' + data[key]);
    alert(key + ' --> ' + data[key]);
}

回答by mythicalcoder

The best and perfect solution for this issue:

此问题的最佳和完美解决方案:

I tried the jQuery with the Ajax success responses, but it doesn't work so I invented my own and finally it works!

我尝试了带有 Ajax 成功响应的 jQuery,但它不起作用,所以我发明了自己的,最后它起作用了!

Click here to see the full solution

单击此处查看完整解决方案

var rs = '{"test" : "Got it perfect!","message" : "Got it!"}';
eval("var toObject = "+ rs + ";");
alert(toObject.message);