0

The below is called from a string url and returns a json object which has an json array inside, but something else is not working, can anyone show me how to access the data inside?

      {"data":[{"8196":{"booking_id":"8150","client_id":"107","venue_id":null}}]
4

3 回答 3

0

You first need to decode the JSON string. The JSON sting you got in return is actually a serialized version of a JSON object.

You need to decode that string into a native Java Object.

There are a number of methods to this in Java. You can start by checking out the Java section at json.org.

于 2013-05-12T12:59:01.583 回答
0
String jsonStr = "{\"data\":[{\"8196\":{\"booking_id\": \"8150\",\"client_id\": \"107\",\"venue_id\": null}}]}";

try {
    JSONObject json = new JSONObject(jsonStr);
    JSONArray array = json.getJSONArray("data");
    for (int i = 0; i < array.length(); i++) {
        JSONObject o = array.getJSONObject(i);
        Iterator it = o.keys();

        while (it.hasNext()) {
            String name = (String) it.next();
            o = o.getJSONObject(name);
            int bookingId = o.getInt("booking_id");
            int clientId = o.getInt("client_id");
            int venueId = o.optInt("venue_id");

            Log.v("TEST", String.format("Booking ID: %s -- Client ID: %s -- Venue ID: %s", bookingId, clientId, venueId));
        }

    }

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

I guess this is what you'll want. By the way, the JSON you posted is malformed. It's missing a } in the end.

于 2013-05-12T13:08:18.430 回答
0

I think the problem is that the value of key venue_id is null. You can replace null value with empty string(""), try it.

于 2013-05-12T13:30:07.373 回答