有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java简单JSON服务器

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

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

我已经试过了,但用户界面没有更新

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) 个答案

  1. # 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();
        }