简单的JSON服务器

时间:2017-11-17 19:24:36

标签: java android json parsing

如何在android中解析这种类型的json:

{
"chatCircles": [{
    "id": "59660c155d44f20b46eab438",
    "name": "Hyderabad Circle",
    "description": "Hyderabad"
}]
}

我试过这个,但UI没有更新。

try {
        JSONArray chatCircleArray = response.getJSONArray("chatCircle");
        for (int i = 0; i < chatCircleArray.length(); i++){
           JSONObject geoFenceObject = chatCircleArray.getJSONObject(i);
           circleId = geoFenceObject.getString("id");
           String circleName = geoFenceObject.getString("name");
           String location = geoFenceObject.getString("description");
           Log.d(TAG, "GeoFenceObject Chat Circle Id parsed is:\t " + circleId);
           System.out.println("Chat Circle id is: \t" + circleId);

            } catch (JSONException e) {
                e.printStackTrace();
            }

请帮助。如何做这个json解析?

1 个答案:

答案 0 :(得分:-1)

第一个大括号是你没有解析过的JSONObject。 请尝试下面的代码。

try {
        JSONObject json = new JSONObject(response);

        JSONArray itemArray = json.getJSONArray("chatCircles");
        for (int i = 0; i < itemArray.length(); i++) {
            JSONObject subJson = (JSONObject) itemArray.get(i);
            String id = subJson.getString("id");
            String name = subJson.getString("name");
            String description = subJson.getString("description");
            Log.i("TEST", "id=" + id + ", name=" + name + ", description=" + description);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }