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

JavascriptJqueryJson

Javascript Problem Overview


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

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

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.

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.

Javascript Solutions


Solution 1 - Javascript

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.

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.

Solution 2 - Javascript

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

Solution 3 - Javascript

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'

Solution 4 - Javascript

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

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

Solution 5 - Javascript

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

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

Solution 6 - Javascript

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!

Click here to see the full solution

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJStarkView Question on Stackoverflow
Solution 1 - JavascriptDarin DimitrovView Answer on Stackoverflow
Solution 2 - JavascriptxdazzView Answer on Stackoverflow
Solution 3 - JavascriptOskarView Answer on Stackoverflow
Solution 4 - JavascriptSmithView Answer on Stackoverflow
Solution 5 - JavascriptmythicalcoderView Answer on Stackoverflow
Solution 6 - Javascriptuser1644502View Answer on Stackoverflow