user3762969
user3762969

Reputation: 23

how to get json value in android

I read many example and try it. But it didn't work to get the value.

The result returned a whole JSON to me.

I don't know where is wrong...

This is my first time to ask question. Thanks for help.

Here is my JSON and JAVA Code.

JSON:`

{
    "food":{
        "name":"XXX Food",
        "details":{
            "code":"01",
            "location":{
                "area":"Area D",
                "place":"Food"
            },
            "price":"132.21",
            "discount":"15%"
        }
    }
}

Now, I want to get the price.

JAVA Code:

JSONObject json = new JSONObject(recvStr);
            JSONArray Results = json.getJSONArray("food");
            JSONArray msg = Results.getJSONArray(0);

            result = msg.getJSONObject(0).getString("price");

Upvotes: 1

Views: 91

Answers (1)

Tobias
Tobias

Reputation: 7771

Results is not a JSONArray but a JSONObject:

JSONObject results = json.getJSONObject("food");
String name = results.getString("name");
JSONObject details = results.getJSONObject("details");
String price = details.getString("price");

You can read more about JSON at json.org.

Upvotes: 1

Related Questions