Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a JSON and turn its values into an Array?

Tags:

java

json

public static void parseProfilesJson(String the_json){        try {             JSONObject myjson = new JSONObject(the_json);              JSONArray nameArray = myjson.names();             JSONArray valArray = myjson.toJSONArray(nameArray);             for(int i=0;i<valArray.length();i++)             {                 String p = nameArray.getString(i) + "," + ValArray.getString(i);                 Log.i("p",p);             }                 } catch (JSONException e) {                 e.printStackTrace();         }     } 

As you can see, this sample code will print out the KEY of the JSONs, followed by the VALUES of the JSONS.

It would print profiles, john if the json was like this:

{'profiles':'john'} 

That's cool. That's fine, as I can work with those variables. However, what if the JSON was like this:

{'profiles': [{'name':'john', 'age': 44}, {'name':'Alex','age':11}]} 

In this case, the entire value would be the array. Basically, I just want to grab that array (which is the "value" in this case)...and turn it into an actual array that JAVA could use. How can I do that? Thanks.

like image 365
TIMEX Avatar asked Feb 12 '10 21:02

TIMEX


People also ask

How do you convert JSON to an array?

Convert JSON to Array Using `json.The parse() function takes the argument of the JSON source and converts it to the JSON format, because most of the time when you fetch the data from the server the format of the response is the string. Make sure that it has a string value coming from a server or the local source.

Can we convert JSON string to array?

Approach 1: First convert the JSON string to the JavaScript object using JSON. Parse() method and then take out the values of the object and push them into the array using push() method.

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.

Can a JSON value be an array?

Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.


1 Answers

for your example:

{'profiles': [{'name':'john', 'age': 44}, {'name':'Alex','age':11}]} 

you will have to do something of this effect:

JSONObject myjson = new JSONObject(the_json); JSONArray the_json_array = myjson.getJSONArray("profiles"); 

this returns the array object.

Then iterating will be as follows:

    int size = the_json_array.length();     ArrayList<JSONObject> arrays = new ArrayList<JSONObject>();     for (int i = 0; i < size; i++) {         JSONObject another_json_object = the_json_array.getJSONObject(i);             //Blah blah blah...             arrays.add(another_json_object);     }  //Finally JSONObject[] jsons = new JSONObject[arrays.size()]; arrays.toArray(jsons);  //The end... 

You will have to determine if the data is an array (simply checking that charAt(0) starts with [ character).

Hope this helps.

like image 76
Buhake Sindi Avatar answered Oct 05 '22 19:10

Buhake Sindi