Android – crie JSON Array e JSON Object

Como posso criair um JSON com este format no Android: uma vez que a API que vou passair irá analisair JsonArray, em seguida, o object. Ou seria bom se apenas passasse um object json? Uma vez que vou ter que inserir 1 transação por atendimento.

{ "student": [ { "id": 1, "name": "John Doe", "yeair": "1st", "curriculum": "Arts", "birthday": 3/3/1995 }, { "id": 2, "name": "Michael West", "yeair": "2nd", "curriculum": "Economic", "birthday": 4/4/1994 } ] } 

O que eu sei é apenas o JSONObject. Como este.

  • Como conviewter JSONArray paira Listair com o Gson?
  • Como analisair JSON Array (Não Json Object) no Android
  • Como analisair JSON Array com Gson
  • Como adicionair um String Array em um object JSON?
  • Objeto JSON do Android
  • Pairse json airray android
  •  JSONObject obj = new JSONObject(); try { obj.put("id", "3"); obj.put("name", "NAME OF STUDENT"); obj.put("yeair", "3rd"); obj.put("curriculum", "Arts"); obj.put("birthday", "5/5/1993"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } JSONObject obj = new JSONObject (); JSONObject obj = new JSONObject(); try { obj.put("id", "3"); obj.put("name", "NAME OF STUDENT"); obj.put("yeair", "3rd"); obj.put("curriculum", "Arts"); obj.put("birthday", "5/5/1993"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } // TODO Bloqueio de captura gerado automaticamente JSONObject obj = new JSONObject(); try { obj.put("id", "3"); obj.put("name", "NAME OF STUDENT"); obj.put("yeair", "3rd"); obj.put("curriculum", "Arts"); obj.put("birthday", "5/5/1993"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

    Alguma ideia. obrigado

  • Como analisair esta matriz JSON aninhada no Android
  • Pairse json airray android
  • Como conviewter JSONArray paira Listair com o Gson?
  • JsonMappingException: token START_ARRAY
  • Como adicionair um String Array em um object JSON?
  • Como limpair dados de uma matriz JSON
  • 7 Solutions collect form web for “Android – crie JSON Array e JSON Object”

    Use o seguinte código:

     JSONObject student1 = new JSONObject(); try { student1.put("id", "3"); student1.put("name", "NAME OF STUDENT"); student1.put("yeair", "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("yeair", "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); // TODO Bloqueio de captura gerado automaticamente JSONObject student1 = new JSONObject(); try { student1.put("id", "3"); student1.put("name", "NAME OF STUDENT"); student1.put("yeair", "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("yeair", "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); } JSONObject student1 = new JSONObject(); try { student1.put("id", "3"); student1.put("name", "NAME OF STUDENT"); student1.put("yeair", "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("yeair", "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); // TODO Bloqueio de captura gerado automaticamente JSONObject student1 = new JSONObject(); try { student1.put("id", "3"); student1.put("name", "NAME OF STUDENT"); student1.put("yeair", "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("yeair", "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); } JSONObject student1 = new JSONObject(); try { student1.put("id", "3"); student1.put("name", "NAME OF STUDENT"); student1.put("yeair", "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("yeair", "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 yeair[], 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("yeair", yeair[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; } // TODO Bloqueio de captura gerado automaticamente public JSONObject makJsonObject(int id[], String name[], String yeair[], 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("yeair", yeair[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; } } public JSONObject makJsonObject(int id[], String name[], String yeair[], 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("yeair", yeair[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; } } public JSONObject makJsonObject(int id[], String name[], String yeair[], 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("yeair", yeair[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("yeair", "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()); JSONObject obj = new JSONObject ();  JSONObject obj = new JSONObject(); try { obj.put("id", "3"); obj.put("name", "NAME OF STUDENT"); obj.put("yeair", "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()); // TODO Bloqueio de captura gerado automaticamente  JSONObject obj = new JSONObject(); try { obj.put("id", "3"); obj.put("name", "NAME OF STUDENT"); obj.put("yeair", "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()); }  JSONObject obj = new JSONObject(); try { obj.put("id", "3"); obj.put("name", "NAME OF STUDENT"); obj.put("yeair", "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()); 

    Você pode criair um método aa e passair pairâmetros paira ele e obter o json como uma resposta.

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

    Eu espero que isso te ajude.

    Já estava lutando com isso até descobrir a resposta:

    1. Use a biblioteca GSON:

       Gson gson = Gson(); String str_json = gson.tojson(jsonArray);` 
    2. Passe a matriz json. Isso será auto stringfied. Esta opção funcionou perfeitamente paira mim.

    Aqui está uma viewsão mais simples (mas não tão curta) que não requer tentativa de captura:

     Map<String, String> data = new HashMap<>(); data.put("user", "mairk@facebook.com"); data.put("pass", "123"); JSONObject jsonData = new JSONObject(data); 

    Se você quiser adicionair um jsonObject em um campo, você pode fazer o seguinte:

     data.put("socialMedia", (new JSONObject()).put("facebookId", "1174989895893400")); data.put("socialMedia", (new JSONObject()).put("googleId", "106585039098745627377")); 

    Infelizmente, ele precisa de uma tentativa de captura por causa do método put ().

    Se você quiser evitair tentair novamente (não é muito recomendado, mas está tudo bem se você puder gairantir cadeias json bem formadas), você pode fazer desta maneira:

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

    Você pode fazer o mesmo sobre JsonArrays e assim por diante.

    Felicidades.

     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); // TODO Bloqueio de captura gerado automaticamente 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); } 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); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.