Eric Teixeira
Eric Teixeira

Reputation: 353

Working with Json in Android

i'm working with Json in Android and this is the json that I recived:

    [
    {
        "0": "5",
        "1": "CorpoSA",
        "2": "CorpoSA",
        "3": "http://162.243.229.85/images/CORPO%20SA%20logo.jpg",
        "id": "5",
        "title": "CorpoSA",
        "description": "CorpoSA",
        "img_url": "http://162.243.229.85/images/CORPO%20SA%20logo.jpg"
    },
    {
        "0": "6",
        "1": "CorpoSA2",
        "2": "CorpoSA2",
        "3": "http://162.243.229.85/images/home.jpg",
        "id": "6",
        "title": "CorpoSA2",
        "description": "CorpoSA2",
        "img_url": "http://162.243.229.85/images/home.jpg"
    }
    ]

I have it in a String named result, but i dont know how to extract the id, title, description and url and for save at database.

When i use:

JSONObject json = new JSONObject(result);

and then

json.getJSONObject("id");

this does not worki, plx someone help me

Upvotes: 0

Views: 104

Answers (1)

Dhaval Patel
Dhaval Patel

Reputation: 10299

try this.

try{
    JSONArray jsonArray = new JSONArray(result);
    // looping through All jsonObject
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        String id = jsonObject.getString("id");
        String title = jsonObject.getString("title");
        String description = jsonObject.getString("description");
        String img_url = jsonObject.getString("img_url");
    }
} catch (JSONException e) {
    e.printStackTrace();
}

Upvotes: 1

Related Questions