4

I didn't found any specific answer which addresses the below question.

I have the following JSON response fetched through an AJAX POST request.

{
"result":
{
"101010":["PRAVAT","SGSGSG","UKEMP5","UKENTD","WAUK01","MK87UK"],
"202020":["CORA1E","PSASAS","EDCRJS","USHC01","USDR06"],
............................
........................
"304050":["ERCDE2","DELT01","DECGKG","DEHC03","IS02","DEPI01"]
},
"status":"SUCCESS"
}

I want to display the data above data by using a loop in javascript. I tried for ( var i = 0; i < response.result.length; i++) { but I am not able to do so.

Please help how can I parse and display my data in the above JSON format using javascript.

4 Answers 4

6

What you have is an object, not an array. Only arrays have the length property. To iterate over an object use:

$.post("yoururlhere", function(JSONData) {
    var obj = $.parseJSON(JSONData);
    if (obj.status.toLowerCase() === "success") {
        for (var key in obj.result) {
            if (obj.result.hasOwnProperty(key)) {
               console.log(key + ': ' + obj.result[key]);
            }
        }
    }
});

The if (obj.result.hasOwnProperty(key)) forces the for to ignore prototype properties. If you care to look it up they are the means you can do inheritance in Javascript.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Hoffmann, when I am using $.parseJSON(json), i am gettin the following error : SyntaxError: JSON.parse: unexpected character
@PravatPanda oh It was missing a closing parentheses on the last line before. I fixed it
@PravatPanda the if was wrong, it is if (obj.result.hasOwnProperty(key)) it was missing the ".result" part. I already fixed it.
Instead of manually parsing it in the callback, just pass "json" as the 4th parameter to $.post(), letting jQuery handle it
4

Do you have it as an object or JSON?

To convert the JSON to an object in jquery, use $.parseJSON().

EG. var obj = $.parseJSON(myJSONData);

Once you have the object, you can loop through the keys using:

for (var key in obj) {
    console.log(key + ': ' + obj[key]);
}

1 Comment

when I am using $.parseJSON(json), i am gettin the following error : SyntaxError: JSON.parse: unexpected character
1

You should parse it, You can use JSON.parse() or jQuery.parseJSON(). Have a look at this: Parse JSON in JavaScript?

1 Comment

Luckily, jQuery determines the best choice of parsing the string. If JSON.parse is available, it uses that. If it's not, it uses it's own way. So if you have jQuery included, it's safe enough to just call jQuery.parseJSON() and the best path should be taken. At the same time, JSON.parse() (and a polyfill for it) more "safely" parses the string, so some might think it's better not to allow jQuery to use its own method
0

As MostafaR said, you need to parse it into a javascript object first. When you get JSON from somewhere, javascript just considers it a string, so you won't be able to access it directly. Also, some older browsers don't have window.JSON, so you'll need to include the json2.js library if you're worried about support from older browsers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.