2013-07-23 170 views
84

如何在Android中使用此格式創建JSON: 由於我將傳遞的API將解析JsonArray,然後解析對象。 或者如果僅僅傳遞一個json對象會好嗎?因爲我只需要爲每個服務呼叫插入一個事務。Android-創建JSON數組和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(); 
} 

任何想法。由於

+1

什麼是isseu? – Blackbelt

+1

[參考鏈接](http://chintankhetiya.wordpress.com/2013/05/27/83/) –

+0

將JSONObject放入JSONArray中以實現發佈的格式.. – sftdev

回答

214

使用下面的代碼:

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

完美的例子! –

4
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()); 
+0

我假設'obj'in'JSONObject obj2 = new JSONObject(); obj.put(「student」,js.toString());'是'obj2'? – sftdev

4

您可以創建一個方法,並傳遞paramters給它,並獲得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; 
     } 

我希望這會幫助你。

21
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; 
    } 
+0

感謝這個簡單的方法。 – KishuDroid

2

一直在努力與此,直到我找到了答案:

  1. 使用GSON庫:

    Gson gson = Gson(); 
    String str_json = gson.tojson(jsonArray);` 
    
  2. 傳遞JSON數組。這將會自動化。這個選項對我來說非常合適。

0
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); 
+2

您應該添加一些敘述來解釋您的答案並改進格式。 – rghome

1

這裏是不需要的try-catch一個簡單的(但不是這麼短)版本:

Map<String, String> data = new HashMap<>(); 
data.put("user", "[email protected]"); 
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。

IF要避免再次的try-catch

(不是很推薦,但它的確定,如果你能保證很好格式化JSON字符串),你可能會做這種方式:

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

你可以做JsonArrays也是如此。

乾杯。