解析json数据。我可以解析这个json数据吗?

时间:2016-05-24 09:42:08

标签: android

我正在尝试解析它,但我无法做到这一点。请帮助我用android中的完整代码解决它

[{"Trip":{"id":"1","trip_start":"2016-05-22 17:20:06","trip_end":"2016-05-22 17:22:19"}}]

2 个答案:

答案 0 :(得分:0)

有很多Google结果会显示这一点,但它会像

JSONArray jsonArray = new JSONArray("[{"Trip":{"id":"1","trip_start":"2016-05-22 17:20:06","trip_end":"2016-05-22 17:22:19"}}]");
JSONObject jsonObject = jsonArray.getJSONObject(0);

JSONObject tripObject = jsonObject.getJSONObject("Trip");

Log.d("JSONResponse", "ID: " + tripObject.getString("id"));

希望这有帮助

答案 1 :(得分:0)

试试这个:

String jsonResponse = "[{\"Trip\":{\"id\":\"1\",\"trip_start\":\"2016-05-22 17:20:06\",\"trip_end\":\"2016-05-22 17:22:19\"}}]";
    try {
        JSONArray jsonArray = new JSONArray(jsonResponse);
        JSONObject jsonObject = jsonArray.getJSONObject(0);
        JSONObject tripObject = jsonObject.getJSONObject("Trip");

        Log.d("JSONResponse", "ID: " + tripObject.getString("id"));
    } catch (JSONException e) {
        e.printStackTrace();
    }

当要解析的JSON很小时,这种方法很好。如果JSON很大,则必须使用JSONReader。有关详细信息,请参阅此文档:https://developer.android.com/reference/android/util/JsonReader.html