2

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" ...

asked Nov 15, 2011 at 9:49
1
  • Are you wanting to treat the array of array of string (String[][]) like a single array of strings (String[])? More like a list that you can iterate over. Commented Nov 15, 2011 at 9:54

8 Answers 8

4
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;
 });
});
answered Nov 15, 2011 at 9:56
Sign up to request clarification or add additional context in comments.

6 Comments

Good example, looks really clean! One suggestion though is to add that using $.each will never be as fast as using native JS and in some cases might not perform as an iterator pure-essence.net/2011/09/02/jquery-each-vs-javascript-for-loop
@leo the efficiency difference is really minor (unless talking about HUGE arrays which I doubt is the case here)
@ShadowWizard Oh, most definitely! But it is up to us to present the best possible solution, which means explaining good practice as well. Not disputing the solution, just my 2 cents.
@ShadowWizard using jQuery to loop through a simple array is like taking a jet to the shop down the street. No need to stick jQuery where it's not needed.
@ShadowWizard jQuery IS Javascript. So I guess you have written an if-else plugin for jQuery aswell? Not only does this method (jQuery for array loops) have problems, that confuse beginners, but it's simply unnecessary. (In your flawed sample, it would still make more sense to go to the store by a car, you'll be home before your neighbors have even taken off from home, and in process you also save money)
|
2
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.

answered Nov 15, 2011 at 9:55

Comments

0

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).

answered Nov 15, 2011 at 9:52

Comments

0
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);
 }
}
answered Nov 15, 2011 at 9:55

Comments

0
for ( var i in json ) 
 for ( var j in json[i] ) 
 alert(json[i][j] )
answered Nov 15, 2011 at 9:57

Comments

0

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]
answered Nov 15, 2011 at 9:59

Comments

0

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.

answered Nov 15, 2011 at 9:55

2 Comments

-1 for lack of formatting and wrong answer. (It will alert "0", not "001", I leave it for you to debug and fix this yourself)
No problem, undone the downvote please take more time in the future to have proper formatting in your posts.
0

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"]
answered Nov 15, 2011 at 10:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.