Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put/get multiple JSONObjects to JSONArray?

Tags:

java

json

Is it possible to store multiple different JSONObjects into a single JSONArray? This is the structure, I want to store in a JSONArray.

[{"value1":1,"value2":900,"value3":1368349},{"value1":2,"value2":1900,"value3":136856},{"value1":3,"value2":600,"value3":136845}] 

Here's the code where I am setting JSONObject and putting it into a JSONArray

if(somecondition) {   // putting values to json object   jsonObj.put("value1", 1);   jsonObj.put("value2", 900);   jsonObj.put("value3", 1368349); } for(int i=0;i<=jsonArray.length();i++){   jsonArray.put(jsonObj);   appObj.setJsonAlarmArray(jsonArray);   // appObj is object of Application Class   editor= sharedPrefs.edit();   editor.putString("key", jsonArray.toString());   System.out.println(jsonArray.toString());   editor.commit(); } 

Using this code only the last value, which I am setting in JSON object Override to all objects. Any suggestions to achieve this?

like image 839
PSK Avatar asked May 11 '13 09:05

PSK


People also ask

How do I combine multiple JSON objects into one?

JSONObject to merge two JSON objects in Java. We can merge two JSON objects using the putAll() method (inherited from interface java.

How do I iterate in JSONArray?

1) Create a Maven project and add json dependency in POM. xml file. 2) Create a string of JSON data which we convert into JSON object to manipulate its data. 3) After that, we get the JSON Array from the JSON Object using getJSONArray() method and store it into a variable of type JSONArray.

Can JSON have multiple objects?

You can pass a single JSON object to create a single element, or a JSON array of group JSON objects to create multiple elements.

How do I get JSONArray object?

JSONArray objects have a function getJSONObject(int index) , you can loop through all of the JSONObjects by writing a simple for-loop: JSONArray array; for(int n = 0; n < array. length(); n++) { JSONObject object = array. getJSONObject(n); // do some stuff.... }


2 Answers

I found very good link for JSON: http://code.google.com/p/json-simple/wiki/EncodingExamples#Example_1-1_-_Encode_a_JSON_object

Here's code to add multiple JSONObjects to JSONArray.

JSONArray Obj = new JSONArray(); try {     for(int i = 0; i < 3; i++) {         // 1st object         JSONObject list1 = new JSONObject();         list1.put("val1",i+1);         list1.put("val2",i+2);         list1.put("val3",i+3);         obj.put(list1);     }  } catch (JSONException e1) {     // TODO Auto-generated catch block     e1.printStackTrace(); }              Toast.makeText(MainActivity.this, ""+obj, Toast.LENGTH_LONG).show(); 
like image 89
PSK Avatar answered Oct 02 '22 08:10

PSK


Once you have put the values into the JSONObject then put the JSONObject into the JSONArray staright after.

Something like this maybe:

jsonObj.put("value1", 1); jsonObj.put("value2", 900); jsonObj.put("value3", 1368349); jsonArray.put(jsonObj); 

Then create new JSONObject, put the other values into it and add it to the JSONArray:

jsonObj.put("value1", 2); jsonObj.put("value2", 1900); jsonObj.put("value3", 136856); jsonArray.put(jsonObj); 
like image 42
Ash Avatar answered Oct 02 '22 08:10

Ash