简体   繁体   English

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

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

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'循环与JSON数组

I also saw the post Get name of key in key/value pair in JSON using jQuery? 我还看到了使用jQuery在JSON中获取键/值对中键的名称? , but it also seemed like lots of code for a simple activity. ,但它似乎也是一个简单活动的代码。

This illustrates what I'm looking for (but it doesn't work): 这说明了我正在寻找的东西(但它不起作用):

var result = '{"FirstName":"John","LastName":"Doe","Email":"johndoe@johndoe.com","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. 没有强制性的jQuery要求,但它是可用的。 I can also restructure the JSON if it cuts down the required code. 如果它减少了所需的代码,我也可以重构JSON。

You have a string representing a JSON serialized JavaScript object. 您有一个表示JSON序列化JavaScript对象的字符串。 You need to deserialize it back to a JavaScript object before being able to loop through its properties. 在能够遍历其属性之前,需要将其反序列化为JavaScript对象。 Otherwise you will be looping through each individual character of this string. 否则,您将循环遍历此字符串的每个单独字符。

var resultJSON = '{"FirstName":"John","LastName":"Doe","Email":"johndoe@johndoe.com","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 . 现场演示

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

You can get the values directly in case of one array like this: 如果是这样的一个数组,您可以直接获取值:

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

The following should work for a JSON returned string. 以下内容适用于JSON返回的字符串。 It will also work for an associative array of data. 它也适用于关联数据数组。

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

Parse the JSON string and you can loop through the keys. 解析JSON字符串,您可以遍历键。

 var resultJSON = '{"FirstName":"John","LastName":"Doe","Email":"johndoe@johndoe.com","Phone":"123 dead drive"}'; var data = JSON.parse(resultJSON); for (var key in data) { //console.log(key + ' : ' + data[key]); alert(key + ' --> ' + data[key]); } 

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! 我尝试了jQuery与Ajax成功响应,但它不起作用所以我发明了自己的,最后它的工作原理!

Click here to see the full solution 单击此处查看完整解决方案

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

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

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