为了账号安全,请及时绑定邮箱和手机立即绑定

Android-创建JSON数组和JSON对象

Android-创建JSON数组和JSON对象

UYOU 2019-10-06 10:51:16
如何在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();}有任何想法吗。谢谢
查看完整描述

3 回答

?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超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);


查看完整回答
反对 回复 2019-10-06
?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

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;

    }


查看完整回答
反对 回复 2019-10-06
?
胡子哥哥

TA贡献1825条经验 获得超6个赞

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


查看完整回答
反对 回复 2019-10-06
  • 3 回答
  • 0 关注
  • 937 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信