Pushpendra Kuntal
Pushpendra Kuntal

Reputation: 6186

How can parse my json result

I am working in android. I want to parse my json data.

This is my json data:-

{

"response":{

  "groups":[

     {

        "type":"nearby",
        "name":"Nearby",
        "items":[
           {
              "id":"4ed0c8f48231b9ef88fe5f09",
              "name":"Banayan Tree School",
              "contact":{

              },
              "location":{
                 "lat":26.857954980225713,
                 "lng":75.76602927296061,
                 "distance":510
              },
              "categories":[
                 {
                    "id":"4bf58dd8d48988d1a8941735",
                    "name":"General College & University",
                    "pluralName":"General Colleges & Universities",
                    "shortName":"Other - Education",
                    "icon":"https:\/\/foursquare.com\/img\/categories\/education\/default.png",
                    "parents":[
                       "Colleges & Universities"
                    ],
                    "primary":true
                 }
              ],
              "verified":false,
              "stats":{
                 "checkinsCount":5,
                 "usersCount":4,
                 "tipCount":0
              },
              "hereNow":{
                 "count":0
              }
           }

        ]
     }
  ]

} }

i want to use icon to show icon in imageview. please suggest me how can i get this icon value and how can i user this icon url in imageview.

Thank you in advance.

i am trying this, but still it is creating error:- this is my code:- but still is creating error:-

JSONArray groups= (JSONArray) jsonObj.getJSONObject("response").getJSONArray("groups");

int length= groups.length(); if (length > 0){ for (int i = 0; i < length; i++)

{

 JSONObject group= (JSONObject) groups.get(i); JSONArray items =(JSONArray) group.getJSONArray("items"); 

for (int j = 0; j < items.length(); j++)

{

JSONObject item = (JSONObject) items.get(j);

JSONObject iconobject=(JSONObject) item.getJSONObject("categories");//this is creating error that JSON.typeMismatch

venue.icon=iconobject.getString("icon");

}}}}

Upvotes: 0

Views: 648

Answers (3)

Bandreid
Bandreid

Reputation: 2733

Try this:

try {
    JSONArray jArray = new JSONArray(result);
    // get into the 'groups' array
    JSONObject jData = jArray.getJSONObject(0);
    JSONArray jGroupsArray = jData.getJSONArray("groups");
    // get into the 'items' array
    jData = jArray.getJSONObject(2);
    JSONArray jItemsArray = jData.getJSONArray("items");
    // get into the 'categories' array
    jData = jArray.getJSONObject(4);
    JSONArray jCategoriesArray = jData.getJSONArray("categories");
    // get into the 'icon' value as String and use it as you please
    jData = jArray.getJSONObject(4);
    String iconURL = jData.getString("icon");
} catch (JSONException e) {
    Log.e(Constants.LOG_TAG, "Error parsing data", e);
}

Hope this helps

Upvotes: 1

micha
micha

Reputation: 49552

The class JSONObject can help you:

String data = ... // your json data
JSONObject json = new JSONObject(data);

You can access nodes in your structure with help of getJSONObject(String) and getJSONArray(String).

For example:

JSONObject response = json.getJSONObject("response");
JSONArray groups = response.getJSONArray("groups");
JSONObject firstGroup = groups.getJSONObject(0);
// and so on

When you got your node that contains your icon value you can use the getString(String) method to get the icon url:

JSONObject firstCategory = categories.getJSONObject(0);
String iconUrl = firstCategory.getString("icon");

After you got the url you have to download the image before you can use it. How to download an image from an url is described here

When you downloaded the image you can update the imageview:

Bitmap image = loadBitmap(iconUrl); // how to implement loadBitmap is shown in the link above
ImageView iv = findViewById(R.id.my_imageview);
iv.setImageBitamp(image);

Upvotes: 2

jcxavier
jcxavier

Reputation: 2232

Refer to the JSON documentation: http://www.json.org/javadoc/org/json/package-summary.html. It's really simple.

In your case, you would have to read the JSON string to a JSON object, then parse "response" as a JSONObject, "groups" as a JSONArray inside "response", iterate through the JSONObjects contained in the "groups" array, parse "Items" as a JSONArray inside your JSONObject, and son on...

You should be able to get to the URL in no time.

Upvotes: 0

Related Questions