Android create JSON array and JSON object

How can I create JSON with this format in Android: Since the API that I will pass will handle JsonArray, then the object. Or would it be nice to just pass the json object? Since I just need to insert 1 transaction per service call.

{ "student": [ { "id": 1, "name": "John Doe", "year": "1st", "curriculum": "Arts", "birthday": 3/3/1995 }, { "id": 2, "name": "Michael West", "year": "2nd", "curriculum": "Economic", "birthday": 4/4/1994 } ] } 

I only know JSONObject. Like this.

 JSONObject obj = new JSONObject(); try { obj.put("id", "3"); obj.put("name", "NAME OF STUDENT"); obj.put("year", "3rd"); obj.put("curriculum", "Arts"); obj.put("birthday", "5/5/1993"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

Any ideas. Thanks

+110
json android arrays
Jul 23 '13 at 12:19
source share
8 answers

Use the following code:

 JSONObject student1 = new JSONObject(); try { student1.put("id", "3"); student1.put("name", "NAME OF STUDENT"); student1.put("year", "3rd"); student1.put("curriculum", "Arts"); student1.put("birthday", "5/5/1993"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } JSONObject student2 = new JSONObject(); try { student2.put("id", "2"); student2.put("name", "NAME OF STUDENT2"); student2.put("year", "4rd"); student2.put("curriculum", "scicence"); student2.put("birthday", "5/5/1993"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } JSONArray jsonArray = new JSONArray(); jsonArray.put(student1); jsonArray.put(student2); JSONObject studentsObj = new JSONObject(); studentsObj.put("Students", jsonArray); String jsonStr = studentsObj.toString(); System.out.println("jsonString: "+jsonStr); 
+288
Jul 23 '13 at 12:31 on
source share
 public JSONObject makJsonObject(int id[], String name[], String year[], String curriculum[], String birthday[], int numberof_students) throws JSONException { JSONObject obj = null; JSONArray jsonArray = new JSONArray(); for (int i = 0; i < numberof_students; i++) { obj = new JSONObject(); try { obj.put("id", id[i]); obj.put("name", name[i]); obj.put("year", year[i]); obj.put("curriculum", curriculum[i]); obj.put("birthday", birthday[i]); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } jsonArray.put(obj); } JSONObject finalobject = new JSONObject(); finalobject.put("student", jsonArray); return finalobject; } 
+25
Nov 22 '13 at 9:24
source share
  JSONObject obj = new JSONObject(); try { obj.put("id", "3"); obj.put("name", "NAME OF STUDENT"); obj.put("year", "3rd"); obj.put("curriculum", "Arts"); obj.put("birthday", "5/5/1993"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } JSONArray js=new JSONArray(obj.toString()); JSONObject obj2 = new JSONObject(); obj2.put("student", js.toString()); 
+5
Jul 23 '13 at 12:30
source share

You can create a method and pass parameters to it and get json as an answer.

  private JSONObject jsonResult(String Name,int id, String curriculum) throws JSONException { JSONObject json = null; json = new JSONObject("{\"" + "Name" + "\":" + "\"" + Name+ "\"" + "," + "\"" + "Id" + "\":" + id + "," + "\"" + "Curriculum" + "\":" + "\"" + curriculum+ "\"" + "}"); return json; } 

Hope this helps you.

+4
Jul 23 '13 at 12:42 on
source share

Here's a simpler (but not so short) version that does not require try-catch:

 Map<String, String> data = new HashMap<>(); data.put("user", "mark@facebook.com"); data.put("pass", "123"); JSONObject jsonData = new JSONObject(data); 

If you want to add jsonObject to the field, you can do this:

 data.put("socialMedia", (new JSONObject()).put("facebookId", "1174989895893400")); data.put("socialMedia", (new JSONObject()).put("googleId", "106585039098745627377")); 

Unfortunately, the put () method needs a try-catch.

IF , you want to avoid try-catch again (not very recommended, but it is ok if you can guarantee well-formed json strings), you can do this:

 data.put("socialMedia", "{ 'facebookId': '1174989895893400' }"); 

You can do the same about JsonArrays and so on.

Greetings.

+3
Jan 26 '17 at 1:54 on
source share

They fought it until I found out the answer:

  • Use the GSON library:

     Gson gson = Gson(); String str_json = gson.tojson(jsonArray);` 
  • Pass the json array. It will be automatically stringfied. This option worked fine for me.

+1
May 28 '15 at 13:53
source share
 JSONObject jsonResult = new JSONObject(); try { jsonResult.put("clave", "valor"); jsonResult.put("username", "iesous"); jsonResult.put("password", "1234"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.d("DEV","jsonResult->"+jsonResult); 
+1
May 19 '16 at 16:00
source share
  Map<String, String> params = new HashMap<String, String>(); //** Temp array List<String[]> tmpArray = new ArrayList<>(); tmpArray.add(new String[]{"b001","book1"}); tmpArray.add(new String[]{"b002","book2"}); //** Json Array Example JSONArray jrrM = new JSONArray(); for(int i=0; i<tmpArray.size(); i++){ JSONArray jrr = new JSONArray(); jrr.put(tmpArray.get(i)[0]); jrr.put(tmpArray.get(i)[1]); jrrM.put(jrr); } //Json Object Example JSONObject jsonObj = new JSONObject(); try { jsonObj.put("plno","000000001"); jsonObj.put("rows", jrrM); }catch (JSONException ex){ ex.printStackTrace(); } // Bundles them params.put("user", "guest"); params.put("tb", "book_store"); params.put("action","save"); params.put("data", jsonObj.toString()); // Now you can send them to the server. 
0
Jun 25 '19 at 1:51
source share



All Articles