2013-07-23 132 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也是如此。

干杯。