从JSONArray获取JSONObject

时间:2011-10-03 11:44:02

标签: java android json

我对作为服务器响应的JSONObject进行了一些修复。

jsonObj = new JSONObject(resultString);
            JSONObject sync_reponse = jsonObj.getJSONObject("syncresponse");
            String synckey_string = sync_reponse.getString("synckey");
            JSONArray createdtrs_array = sync_reponse.getJSONArray("createdtrs");
            JSONArray modtrs_array = sync_reponse.getJSONArray("modtrs");
            JSONArray deletedtrs_array = sync_reponse.getJSONArray("deletedtrs");
            String deleted_string = deletedtrs_array.toString();

{"syncresponse":{"synckey":"2011-09-30 14:52:00","createdtrs":[],"modtrs":[],"deletedtrs":[{"companyid":"UTB17","username":"DA","date":"2011-09-26","reportid":"31341"}]

你可以在我得到的响应中看到我正在解析JSONObject并创建syncresponse,synckey作为 JSON对象 createdtrs,modtrs,deletedtrs作为 JSONArray 。我想从deletedtrs访问JSONObject,以便我可以拆分它们并使用这些值。即我想提取companyid,用户名,日期等。

我该如何解决这个问题?

感谢您的意见。

7 个答案:

答案 0 :(得分:115)

JSONArray对象有一个函数getJSONObject(int index),您可以通过编写一个简单的for循环来遍历所有JSONObject:

JSONArray array;
for(int n = 0; n < array.length(); n++)
{
    JSONObject object = array.getJSONObject(n);
    // do some stuff....
}

答案 1 :(得分:17)

这是你的json:

{
    "syncresponse": {
       "synckey": "2011-09-30 14:52:00",
        "createdtrs": [

        ],
        "modtrs": [

        ],
        "deletedtrs": [
          {
           "companyid": "UTB17",
           "username": "DA",
           "date": "2011-09-26",
           "reportid": "31341"
      }
       ]
   }
}

它正在解析:

JSONObject object = new JSONObject(result);
String syncresponse = object.getString("syncresponse");
JSONObject object2 = new JSONObject(syncresponse);
String synckey = object2.getString("synckey");
JSONArray jArray1 = object2.getJSONArray("createdtrs");
JSONArray jArray2 = object2.getJSONArray("modtrs");
JSONArray jArray3 = object2.getJSONArray("deletedtrs");
for(int i = 0; i < jArray3 .length(); i++)
{
   JSONObject object3 = jArray3.getJSONObject(i);
   String comp_id = object3.getString("companyid");
   String username = object3.getString("username");
   String date = object3.getString("date");
   String report_id = object3.getString("reportid");
}

答案 2 :(得分:1)

JSONArray deletedtrs_array = sync_reponse.getJSONArray("deletedtrs");

for(int i = 0; deletedtrs_array.length(); i++){

            JSONObject myObj = deletedtrs_array.getJSONObject(i);
}

答案 3 :(得分:1)

{"syncresponse":{"synckey":"2011-09-30 14:52:00","createdtrs":[],"modtrs":[],"deletedtrs":[{"companyid":"UTB17","username":"DA","date":"2011-09-26","reportid":"31341"}]

获取companyid,用户名,日期;

jsonObj.syncresponse.deletedtrs[0].companyid
jsonObj.syncresponse.deletedtrs[0].username
jsonObj.syncresponse.deletedtrs[0].date

答案 4 :(得分:1)

开始
JSONArray deletedtrs_array = sync_reponse.getJSONArray("deletedtrs");

您可以遍历JSONArray并直接使用值或创建您自己类型的对象
它将处理每个deletedtrs_array成员

内的数据字段

<强>迭代

for(int i = 0; i < deletedtrs_array.length(); i++){
    JSONObject obj = deletedtrs_array.getJSONObject(i);
    Log.d("Item no."+i, obj.toString());

    // create object of type DeletedTrsWrapper like this
    DeletedTrsWrapper dtw = new DeletedTrsWrapper(obj);

    // String company_id = obj.getString("companyid");
    // String username = obj.getString("username");
    // String date = obj.getString("date");
    // int report_id = obj.getInt("reportid");
}

拥有对象类型

class DeletedTrsWrapper {

    public String company_id;
    public String username;
    public String date;
    public int report_id;

    public DeletedTrsWrapper(JSONObject obj){
        company_id = obj.getString("companyid");
        username = obj.getString("username");
        date = obj.getString("date");
        report_id = obj.getInt("reportid");
    }
}

答案 5 :(得分:1)

使用google gson库时。

var getRowData =
[{
    "dayOfWeek": "Sun",
    "date": "11-Mar-2012",
    "los": "1",
    "specialEvent": "",
    "lrv": "0"
},
{
    "dayOfWeek": "Mon",
    "date": "",
    "los": "2",
    "specialEvent": "",
    "lrv": "0.16"
}];

    JsonElement root = new JsonParser().parse(request.getParameter("getRowData"));
     JsonArray  jsonArray = root.getAsJsonArray();
     JsonObject  jsonObject1 = jsonArray.get(0).getAsJsonObject();
     String dayOfWeek = jsonObject1.get("dayOfWeek").toString();

//使用jackson库时

    JsonFactory f = new JsonFactory();
              ObjectMapper mapper = new ObjectMapper();
          JsonParser jp = f.createJsonParser(getRowData);
          // advance stream to START_ARRAY first:
          jp.nextToken();
          // and then each time, advance to opening START_OBJECT
         while (jp.nextToken() == JsonToken.START_OBJECT) {
            Map<String,Object> userData = mapper.readValue(jp, Map.class);
            userData.get("dayOfWeek");
            // process
           // after binding, stream points to closing END_OBJECT
        }

答案 6 :(得分:0)

尽可能多地使用Android Volly库。它将您的JSON响应映射到相应的类对象中。您可以为该响应模型对象添加getter setter。然后,您可以使用.operator访问这些JSON值/参数,就像普通的JAVA对象一样。它使响应处理变得非常简单。