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.

Pravat Panda
  • 1,060
  • 2
  • 13
  • 27

4 Answers4

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.

Hoffmann
  • 14,369
  • 16
  • 76
  • 91
  • Hi Hoffmann, when I am using $.parseJSON(json), i am gettin the following error : SyntaxError: JSON.parse: unexpected character – Pravat Panda Jun 05 '13 at 14:09
  • @PravatPanda oh It was missing a closing parentheses on the last line before. I fixed it – Hoffmann Jun 05 '13 at 14:51
  • @PravatPanda the if was wrong, it is if (obj.result.hasOwnProperty(key)) it was missing the ".result" part. I already fixed it. – Hoffmann Jun 05 '13 at 17:43
  • 2
    Instead of manually parsing it in the callback, just pass "json" as the 4th parameter to `$.post()`, letting jQuery handle it – Ian Jun 06 '13 at 02:08
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]);
}
itsmejodie
  • 4,148
  • 1
  • 18
  • 20
1

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

Community
  • 1
  • 1
MostafaR
  • 3,547
  • 1
  • 17
  • 24
  • 3
    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 – Ian Jun 05 '13 at 13:32
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.

Jedediah
  • 1,916
  • 16
  • 32