android - Android - 创建JSON数组和JSON对象

如何在Android中使用这种格式创建JSON:因为我将要传递的API将解析成JsonArray对象。因为我只需要在每个服务调用中插入一个事务。


{


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



你可以创建一个方法,并且向它传递参数,然后获取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;


 }



我希望这对你有帮助。

使用GSON库:


Gson gson = Gson();


String str_json = gson.tojson(jsonArray);`



传递json数组,这会被自动字符串化。


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




 Map<String, String> params = new HashMap<String, String>();



 List<String[]> tmpArray = new ArrayList<>();


 tmpArray.add(new String[]{"b001","book1"}); 


 tmpArray.add(new String[]{"b002","book2"}); 



 JSONArray jrrM = new JSONArray();


 for(int i=0;i<adt.getListData().size();i++){


 JSONArray jrr = new JSONArray();


 jrr.put(tmpArray.get(i)[0]);


 jrr.put(tmpArray.get(i)[1]);


 jrrM.put(jrr);


 }



 params.put("file","op/getdata");


 params.put("func","savePick");


 params.put("data", jrrM.toString());



...