Android-创建JSON数组和JSON对象

如何在Android中以这种格式创建JSON:由于我将传递的API将解析JsonArray,然后解析该对象。还是只传递一个json对象就可以了吗?因为我将只需要为每个服务调用插入1个事务。


{

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

}

有任何想法吗。谢谢


UYOU
浏览 938回答 3
3回答

泛舟湖上清波郎朗

使用以下代码: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);

犯罪嫌疑人X

public JSONObject makJsonObject(int id[], String name[], String year[],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String curriculum[], String birthday[], int numberof_students)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws JSONException {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject obj = null;&nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonArray = new JSONArray();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < numberof_students; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj = new JSONObject();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.put("id", id[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.put("name", name[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.put("year", year[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.put("curriculum", curriculum[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.put("birthday", birthday[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated catch block&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonArray.put(obj);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; JSONObject finalobject = new JSONObject();&nbsp; &nbsp; &nbsp; &nbsp; finalobject.put("student", jsonArray);&nbsp; &nbsp; &nbsp; &nbsp; return finalobject;&nbsp; &nbsp; }

胡子哥哥

&nbsp;JSONObject obj = new JSONObject();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.put("id", "3");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.put("name", "NAME OF STUDENT");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.put("year", "3rd");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.put("curriculum", "Arts");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.put("birthday", "5/5/1993");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated catch block&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONArray js=new JSONArray(obj.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONObject obj2 = new JSONObject();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;obj2.put("student", js.toString());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android