Javascript how to parse a JSON array

I am using Sencha Touch (ExtJS) to receive a JSON message from the server. The message I receive is the following:

{ "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 cannot parse this JSON object so that I can use each of the counter objects.

I’m trying to understand 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!

+74
json javascript extjs
Apr 03 2018-12-12T00:
source share
9 answers

Javascript has built-in JSON analysis for strings, which I think has what you have:

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

to use this with your example:

 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 : error in using for loop (I missed this on first read, @Evert credit for place). using a for-in loop will set var as the property name of the current loop, not the actual data. See My Updated Cycle Above for Correct Use.

IMPORTANT : the JSON.parse method JSON.parse not work in old old browsers, so if you plan to make your site accessible after some time by bending the Internet connection, this can be a problem! If you are really interested, here is a support schedule (which ticks all my boxes).

+120
Apr 3 2018-12-12T00:
source share

In a for-in-loop, the current variable contains the name of the property, not the value of the property.

 for (var counter in jsonData.counters) { console.log(jsonData.counters[counter].counter_name); } 

But since counters are arrays, you should use a regular for loop:

 for (var i=0; i<jsonData.counters.length; i++) { var counter = jsonData.counters[i]; console.log(counter.counter_name); } 
+6
Apr 03 '12 at 11:00
source share

This is my answer,

 <!DOCTYPE html> <html> <body> <h2>Create Object from JSON String</h2> <p> First Name: <span id="fname"></span><br> Last Name: <span id="lname"></span><br> </p> <script> var txt = '{"employees":[' + '{"firstName":"John","lastName":"Doe" },' + '{"firstName":"Anna","lastName":"Smith" },' + '{"firstName":"Peter","lastName":"Jones" }]}'; //var jsonData = eval ("(" + txt + ")"); var jsonData = JSON.parse(txt); for (var i = 0; i < jsonData.employees.length; i++) { var counter = jsonData.employees[i]; //console.log(counter.counter_name); alert(counter.firstName); } </script> </body> </html> 
+5
Mar 19 '13 at 13:01
source share

"Sencha way" configures Ext.data.Store , Ext.data.proxy.Proxy (in this case Ext.data.proxy.Ajax ), equipped with Ext.data.reader.Json (for JSON-encoded) to interact with server data data there are other readers also available). There are several types of Ext.data.writer.Writer for writing data to the server.

Here is an example of such a setting:

  var store = Ext.create('Ext.data.Store', { fields: [ 'counter_name', 'counter_type', 'counter_unit' ], proxy: { type: 'ajax', url: 'data1.json', reader: { type: 'json', idProperty: 'counter_name', rootProperty: 'counters' } } }); 

data1.json in this example (also available in this fiddle ) contains your data verbatim. idProperty: 'counter_name' is probably optional in this case, but usually points to a primary key attribute. rootProperty: 'counters' indicates which property contains an array of data items.

With the storage setup this way, you can re-read the data from the server by calling store.load() . You can also connect the storage to any components of the Sencha Touch user interface, such as grids, lists, or forms.

+1
May 29 '17 at 10:47 p.m.
source share

Something more for me ..

 var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}'; var contact = JSON.parse(jsontext); document.write(contact.surname + ", " + contact.firstname); document.write(contact.phone[1]); // Output: // Aaberg, Jesper // 555-0100 

Link: https://docs.microsoft.com/en-us/scripting/javascript/reference/json-parse-function-javascript

0
May 29 '17 at 21:21
source share

It works like a charm!

So I edited the code according to my requirement. And here are the changes: it will save the identification number from the response to the environment variable.

 var jsonData = JSON.parse(responseBody); for (var i = 0; i < jsonData.data.length; i++) { var counter = jsonData.data[i]; postman.setEnvironmentVariable("schID", counter.id); } 
0
Jul 25 '18 at 13:21
source share

You must use the data warehouse and proxy server in ExtJs. There are many examples of this, and the JSON reader will automatically parse the JSON message in the model you specify.

There is no need to use basic Javascript when using ExtJs, everything is different, you have to use ExtJs so that everything is correct. Read the documentation carefully, this is good.

By the way, these examples also apply to Sencha Touch (especially v2), which is based on the same basic functions as ExtJs.

-one
Apr 3 2018-12-12T00:
source share

Not sure if my data matches exactly, but I had an array of JSON object arrays that were exported from jQuery FormBuilder when using pages.

I hope my answer will help anyone who stumbles upon this question in search of an answer to a problem similar to mine.

The data looked something like this:

 var allData = [ [ { "type":"text", "label":"Text Field" }, { "type":"text", "label":"Text Field" } ], [ { "type":"text", "label":"Text Field" }, { "type":"text", "label":"Text Field" } ] ] 

To figure this out, I simply did the following:

 JSON.parse("["+allData.toString()+"]") 
-one
May 30 '18 at 14:08
source share

An answer with a higher voice has an error. when I used it, I found out about it on line 3:

 var counter = jsonData.counters[i]; 

I changed this to:

 var counter = jsonData[i].counters; 

and it worked for me. There is a difference with the other answers on line 3:

 var jsonData = JSON.parse(myMessage); for (var i = 0; i < jsonData.counters.length; i++) { var counter = jsonData[i].counters; console.log(counter.counter_name); } 
-one
May 05 '19 at 11:43
source share



All Articles