Pir Fahim Shah
Pir Fahim Shah

Reputation: 10623

Fetch value from JSON in android

I am sending request to the server and it gives me a response and giving data in JSON formats, now i want to fetch some specific value from that JSON format, so hot to do it.

{
"education": [
{
  "school": {
    "id": "2009305", 
    "name": "FG boys public high school Bannu Cantt "
  }, 
  "type": "High School"
}, 
{
  "school": {
    "id": "109989", 
    "name": "University of Engineering & Technology"
  }, 
  "type": "College"
}
], 
"id": "xxxxxxx"
}

Now i need the school names from this JSON

Upvotes: 1

Views: 61

Answers (2)

chimia
chimia

Reputation: 28

first build a JSONobject from your data:

JSONObject jsonObj = new JSONObject(result); //result = your Data String in fetched from server

then you cab retrieve what you want using its key. for example:

jsonObj.getString("id"); // it returns "xxxxxxx". as is in your data

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

JSONObject json = new JSONObject(response);
JSONArray education = json.getJSONArray("education");
for(int i = 0; i < education.length(); i++){
     JSONObject con_json = education.getJSONObject(i);
     String school_type = con_json.getString("type");
     JSONObject school_json = con_json.getJSONObject("school");
     String school_name = school_json.getString("name");
}

Upvotes: 1

Related Questions