JSON RESPONSE IN ANDROID

Jyotishgher Astrology
By -
0

Below is the sample code of JSON. Its very simple JSON code which gives us the list of users where each object contain the information like user id, name, email, gender and different contact numbers.

IF J SON RESPONSE LIKE THIS:



{"asc_report":{"ascendant":"Cancer","report":"People wigfgdgdfgdfgfgfghggfhghgh"}}


Then Java logic To get Single object value will be:




JSONObject jObj = new JSONObject(response);
JSONObject asc_report = jObj.getJSONObject("asc_report");
String ascendant =asc_report.getString("ascendant");
Log.d("asc_report_lagna", "onResponse: " +ascendant);
CASE 2
{
    "users": [
        {
                "id": "1087",
                "name": "Abhishek Saini",
                "email": "info@abhiandroid.com",
                "gender" : "male",
                "contact": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "1088",
                "name": "Gourav",
                "email": "gourav9188@gmail.com",
                "gender" : "male",
                "contact": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        .
        .
        .
        .
  ]
}
CODE TO DO IN ANDROID

   // get JSONObject from JSON file
            JSONObject obj = new JSONObject(JSON_STRING);
            // fetch JSONObject named employee
            JSONObject employee = obj.getJSONObject("employee");
            // get employee name and salary
            name = employee.getString("name");
            salary = employee.getString("salary");
            // set employee name and salary in TextView's
            employeeName.setText("Name: "+name);
            employeeSalary.setText("Salary: "+salary)

Post a Comment

0Comments

Post a Comment (0)