问题描述:

I am trying to parse it but i can not do that.please help me to solve it with full code in android

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

网友答案:

There are a lot of Google results that would show this but it would be something like

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"));

Hope this helps

网友答案:

Try this :

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

This approach is good when the JSON to be parsed is small. If the JSON is large you must use JSONReader. Go through this document for more details : https://developer.android.com/reference/android/util/JsonReader.html

相关阅读:
Top