Android-创buildJSON数组和JSON对象

我如何在Android中使用这种格式创buildJSON:由于我将传递的API将parsingJsonArray,然后parsing对象。 或者,如果只是传递一个json对象,它会好吗? 因为我只需要为每个服务调用插入一个事务。

{ "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 } ] } 

我知道的只是JSONObject。 像这个。

 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(); } 

有任何想法吗。 谢谢

使用下面的代码:

 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); 
 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; } 
  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()); 

你可以创build一个方法并将parameter passing给它,并获取json作为响应。

  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; } 

我希望这会帮助你。

一直在挣扎,直到我find答案:

  1. 使用GSON库:

     Gson gson = Gson(); String str_json = gson.tojson(jsonArray);` 
  2. 传递json数组。 这将会自动化。 这个选项对我来说是完美的。

这是一个更简单(但不是很短)的版本,不需要try-catch:

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

如果你想添加一个jsonObject到一个字段,你可以这样做:

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

不幸的是,由于put()方法,它需要一个try-catch。

如果你想避免再试一次(不是很推荐,但是如果你能保证格式化的JSONstring没关系),你可以这样做:

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

你可以做同样的JsonArrays等等。

干杯。

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