如何创建对象的JSONObject? Android的

时间:2015-11-19 15:13:12

标签: java android json jsonobject

我想像这样创建一个JSONObject

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

这是我的代码:

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

EquipmentViewed包含String列表设备。

3 个答案:

答案 0 :(得分:4)

您可以使用以下方法创建所需的JSON:

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
               }
           ]
  */

答案 1 :(得分:1)

您可以使用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
    }

如果您的equipmentViewed.getEquipment()。toString()返回以下内容:

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

答案 2 :(得分:1)

您可以尝试这样的事情:

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

您将拥有JsonArray对象jsonArray中的所有数据。 我假设你equipmentViewed.getEquipment()列表中的对象有这些getter方法。

一切顺利:)