Iqbal
Iqbal

Reputation: 245

Loop Through JSON Data

I have JSON Data return by JQuery -

[["001","Item1","2011-03-15","2011-06-15"],["001","Item2","2011-07-15","2011-11-15"]]

How can I get each value "001", "Item1" ...

Upvotes: 2

Views: 6539

Answers (8)

Adam Zalcman
Adam Zalcman

Reputation: 27233

In jQuery you can flatten your array using map:

var data = $.map(your_array_from_json, function(e) { return e; });

After this data is a one-dimensional array containing all elements in your data, e.g.:

["001","Item1","2011-03-15","2011-06-15","001","Item2","2011-07-15","2011-11-15"]

Upvotes: 0

leo.vingi
leo.vingi

Reputation: 1852

var json_data = [["001","Item1","2011-03-15","2011-06-15"],["001","Item2","2011-07-15","2011-11-15"]];
var json_length = json_data.length;
var inner_length = 0;

for (var i = 0; i<json_length; i++)
{
    inner_length = json_data[i].length;
    for( var j = 0; j<inner_length; j++ ){
        alert(json_data[i][j]);
    }
}

I noticed that it is not really a JSON object, but it's an array structure. So we just have 2 for loops, to get all the values. The code above should display all the values you have in those arrays.

Also, you should always define the array length somewhere outside the for loops. Makes for better code and faster execution, because you are not calling the length method every time.

EDIT: I would just like to add that using $.each might not be the preferred method in some cases: http://pure-essence.net/2011/09/02/jquery-each-vs-javascript-for-loop/

Using native JS will always be faster or at least on par with jQuery.

Upvotes: 2

Abhinav
Abhinav

Reputation: 1091

Suppose that:

    var x = [["001","Item1","2011-03-15","2011-06-15"],["001","Item2","2011-07-15","2011-11-15"]];

Then this:

    alert(x[0][0]);

Will alert "001" Value.

Upvotes: 0

Ramandeep Singh
Ramandeep Singh

Reputation: 5253

This is not a real JSON. Real JSON will look like :

[{"key":"value"},{"key":"value"}]

Yours is just an Array. So just iterate through TWO loops to print.

But if you want to iterate JSON Data ...do the following :


If you are using JAVA, then you need to do the following :

import org.json.JSONArray;

public some_function () {

try {
   String json = "you json string";
   JSONArray jsonArr = new JSONArray (json);
}
catch (org.json.JSONException e) {
}
for (int i=0; i<jsonArray.length();j++)  {
     JSONObject json = new JSONObject(jsonArr.get(i));
     JSONArray names= json.names();
     JSONArray values = json.toJSONArray(names);

     for (int j=0;j<values.length();j++) 
          //print "values";
     }
}

If you are using Python , do this :

import simplejson as json

json_data = json.loads ("you json string")

for (json in json_data) :
    print json[0]

Upvotes: 0

Piotr
Piotr

Reputation: 1753

for ( var i in json ) 
 for ( var j in json[i] ) 
  alert(json[i][j] )

Upvotes: 0

RvdK
RvdK

Reputation: 19790

var json = [["001","Item1","2011-03-15","2011-06-15"],["001","Item2","2011-07-15","2011-11-15"]];
$.each(json, function(key1, item) {
  $.each(item, function(key2, itemvalues) {
    var test = itemvalues;
  });
});

Upvotes: 5

IM_AG
IM_AG

Reputation: 518

var data = [["001", "Item1","2011-03-15"],["001", "Item2","2011-07-15"]];

for ( var i = 0, data_len = data.length; i < data; i++ ) {
    for ( var j = 0, item_len = data[i].length; j < item_len; j++ ) {
         var item = data[i][j];
         console.log(item);
    }
}

Upvotes: 0

user971401
user971401

Reputation:

You have to parse that JSON, and iterate on array's elements.

var datas = JSON.parse(yourJSON); // you get an array of arrays
var firstArray = datas[0];        // get the first of them
for(var i = 0, len = firstArray.length; i < len; i += 1) {
    // you access the elements
}

Do the same for the others (in a loop or something).

Upvotes: 0

Related Questions