How to create a JSONObject? Android

I want to create JSONObjectas follows:

[
   {
       id:01,
       name:"John",
       number:010
   },
   {
       id:02,
       name:"Mike",
       number: 020
   }
]

This is my code:

public void equipmentViewed(List<Equipment> equipmentSelected, final OnControlResponseListener listener, String description, Equipment equipment) throws JSONException {
        wsAccessControl = WSAccessControl.getInstance();

        EquipmentViewed equipmentViewed = new EquipmentViewed();
        equipmentViewed.setEquipment(equipmentsCount(equipmentSelected));

        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("", new JSONArray(equipmentViewed.getEquipment().toString()));
        } catch (JSONException e) {
            Log.e(TAG, "Failed to create json object. Cause: " + e.getMessage());
        }
        String url = Constants.PROVIDER_DOMAIN_URL + Constants.REQUEST_EQUIPMENT;
        wsAccessControl.makeWSRequest(RequestType.POST, url, jsonObject, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                listener.OnResponseReceived(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                listener.OnResponseError(error);
            }
        }, true);
    }

Where EquipmentViewedcontains the equipment list String.

+4
source share
3 answers

You can create the JSON you want to use:

JSONArray array = new JSONArray();

JSONObject obj1 = new JSONObject();
obj1.put("id", "01");
obj1.put("name", "John");
obj1.put("number", "010");

JSONObject obj2 = new JSONObject();
obj2.put("id", "02");
obj2.put("name", "Mike");
obj2.put("number", "020");

array.put(obj1);
array.put(obj2);

/* array = [
              {
                   id:01,
                   name:"John",
                   number:010
               },
               {
                   id:02,
                   name:"Mike",
                   number: 020
               }
           ]
  */
+5
source

You can try something like this:

JsonArray jsonArray = new JsonArray();
for (int i = 0; i < equipmentViewed.getEquipment().size(); i++) {
    JsonObject jsonObject = new JsonObject();
    jsonObject.put("id", equipmentViewed.getEquipment().get(i).getId());
    jsonObject.put("name", equipmentViewed.getEquipment().get(i).getName());
    jsonObject.put("number", equipmentViewed.getEquipment().get(i).getNumber());
    jsonArray.put(jsonObject);
}

You will have all the data in the JsonArray object jsonArray. I assume the objects in your list equipmentViewed.getEquipment()have these getter methods.

All the best :)

+2
source

JSONTokener .

    try{
        String json = equipmentViewed.getEquipment().toString();
        JSONArray object = (JSONArray) new JSONTokener(json).nextValue();
        JSONObject firstEntry = (JSONObject) object.get(0);
        JSONObject sndEntry = (JSONObject) object.get(1);
    }catch (JSONException ex){
      //TODO handle Error here
    }

Viewed.getEquipment(). toString() :

   /* String json =
   [
       {
           id:01,
           name:"John",
           number:010
       },
       {
           id:02,
           name:"Mike",
           number: 020
       }
    ] */
+1

Source: https://habr.com/ru/post/1616571/


All Articles