Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript how to parse JSON array

I'm using Sencha Touch (ExtJS) to get a JSON message from the server. The message I receive is this one :

{ "success": true, "counters": [     {         "counter_name": "dsd",         "counter_type": "sds",         "counter_unit": "sds"     },     {         "counter_name": "gdg",         "counter_type": "dfd",         "counter_unit": "ds"     },     {         "counter_name": "sdsData",         "counter_type": "sds",         "counter_unit": "   dd       "     },     {         "counter_name": "Stoc final",         "counter_type": "number    ",         "counter_unit": "litri     "     },     {         "counter_name": "Consum GPL",         "counter_type": "number    ",         "counter_unit": "litri     "     },     {         "counter_name": "sdg",         "counter_type": "dfg",         "counter_unit": "gfgd"     },     {         "counter_name": "dfgd",         "counter_type": "fgf",         "counter_unit": "liggtggggri     "     },     {         "counter_name": "fgd",         "counter_type": "dfg",         "counter_unit": "kwfgf       "     },     {         "counter_name": "dfg",         "counter_type": "dfg",         "counter_unit": "dg"     },     {         "counter_name": "gd",         "counter_type": "dfg",         "counter_unit": "dfg"     }      ] } 

My problem is that I can't parse this JSON object so that i can use each of the counter objects.

I'm trying to acomplish that like this :

var jsonData = Ext.util.JSON.decode(myMessage); for (var counter in jsonData.counters) {      console.log(counter.counter_name);  } 

What am i doing wrong ? Thank you!

like image 699
maephisto Avatar asked Apr 03 '12 10:04

maephisto


People also ask

How do you parse an array in JSON?

Use the JSON. parse() method to pase a JSON array, e.g. JSON. parse(arr) . The method parses a JSON string and returns its JavaScript value or object equivalent.

Does JSON parse return an array?

When using the JSON.parse() on a JSON derived from an array, the method will return a JavaScript array, instead of a JavaScript object.

What is [] and {} in JSON?

' { } ' used for Object and ' [] ' is used for Array in json.

Can you JSON Stringify an array?

Stringify a JavaScript ArrayIt is also possible to stringify JavaScript arrays: Imagine we have this array in JavaScript: const arr = ["John", "Peter", "Sally", "Jane"]; Use the JavaScript function JSON.


1 Answers

Javascript has a built in JSON parse for strings, which I think is what you have:

var myObject = JSON.parse("my json string"); 

to use this with your example would be:

var jsonData = JSON.parse(myMessage); for (var i = 0; i < jsonData.counters.length; i++) {     var counter = jsonData.counters[i];     console.log(counter.counter_name); } 

Here is a working example

EDIT: There is a mistake in your use of for loop (I missed this on my first read, credit to @Evert for the spot). using a for-in loop will set the var to be the property name of the current loop, not the actual data. See my updated loop above for correct usage

IMPORTANT: the JSON.parse method wont work in old old browsers - so if you plan to make your website available through some sort of time bending internet connection, this could be a problem! If you really are interested though, here is a support chart (which ticks all my boxes).

like image 62
musefan Avatar answered Oct 01 '22 07:10

musefan