如何创建包含对象的JSONObject?Android

4
我希望能创建一个像这样的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 列表 equipment。


equipmentViewed.getEquipment() 返回的是什么字符串?请分享。 - ρяσѕρєя K
它是一份包含信息列表的程序相关内容,例如,名称,型号,序列号,位置。 - Eduardo Bonfa
3个回答

5
你可以使用以下方法创建想要的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
               }
           ]
  */

我使用了Gson,这样更容易。但无论如何还是谢谢你。 - Eduardo Bonfa

2
您可以使用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
       }
    ] */

但在这种情况下,会创建类似于以下内容的东西:{ id: [ { 010; 020 } ],对吧? - Eduardo Bonfa
抱歉,我误解了你的问题并编辑了答案。这样解决了你的问题吗? - woley
我使用了Gson,这样更容易。但无论如何还是谢谢你。 - Eduardo Bonfa

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方法。
祝一切顺利 :)

我使用了Gson,这样更容易。但无论如何还是谢谢你。 - Eduardo Bonfa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接