4
{
    "status": "Success",
    "message": "Contents retrieved successfully",
    "name": {
        "1": "God",
        "2": "Goat"
    },
    "sites": {
        "1": "google",
        "2": "yahoo",
        "3": "bing"
    },
    "places": [
        "UK",
        "AU",
        "US"
    ],
    "images": {
        "1": {
            "1x": "http://3.bp.blogspot.com/-PPrUA_pcNyI/Udtx6v7MlvI/AAAAAAAADZA/6X2Qu-FcHtA/s320/Android+JSON+stream+data+parsing+example+using+Gson.png",
            "2x": "http://3.bp.blogspot.com/-PPrUA_pcNyI/Udtx6v7MlvI/AAAAAAAADZA/6X2Qu-FcHtA/s320/Android+JSON+stream+data+parsing+example+using+Gson.png"
        },
        "2": {
            "1x": "http://3.bp.blogspot.com/-PPrUA_pcNyI/Udtx6v7MlvI/AAAAAAAADZA/6X2Qu-FcHtA/s320/Android+JSON+stream+data+parsing+example+using+Gson.png",
            "2x": "http://3.bp.blogspot.com/-PPrUA_pcNyI/Udtx6v7MlvI/AAAAAAAADZA/6X2Qu-FcHtA/s320/Android+JSON+stream+data+parsing+example+using+Gson.png"
        },
        "3": {
            "1x": "http://3.bp.blogspot.com/-PPrUA_pcNyI/Udtx6v7MlvI/AAAAAAAADZA/6X2Qu-FcHtA/s320/Android+JSON+stream+data+parsing+example+using+Gson.png",
            "2x": "http://3.bp.blogspot.com/-PPrUA_pcNyI/Udtx6v7MlvI/AAAAAAAADZA/6X2Qu-FcHtA/s320/Android+JSON+stream+data+parsing+example+using+Gson.png"
        }
    }
}

My Class

import java.util.Map;

public class Data {

    String status;
    String message;
    Map<String, String> name;
    Map<String, String> Sites;
    @Override
    public String toString() {
        return "Data [status=" + status + ", message=" + message
                + ", name=" + name + ", Sites=" + Sites
                + "]";
    }


}

this class returns null value for the while retrieving sites and names

Venkatesh S
  • 5,466
  • 1
  • 25
  • 30

2 Answers2

1

name and sites are JSONObjects no Arrays. Any Object in a JSON have to deserialised in a class using GSON.

So try this,

public class MyJson {
    String status;
    String message;

    Sites sites;
    List<String> places;
}

public class Sites {
    String 1;
    String 2;
    String 3;
}

and so on for every Object. For Arrays you can use List / Map.

To use it make a call like this:

Gson gson = new Gson();
MyJson myJson = gson.fromJson(yourJsonString, MyJson.class);
Alonso Dominguez
  • 7,750
  • 1
  • 27
  • 37
Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • this could work (particularly after removing the brackets at the classes definition), but it's not flexible enough as it seems that the JSON object can have an arbitrary number of elements for the keys `name`, `sites`, `places` and `images` (and for the contents of the latter too) – Alonso Dominguez Aug 08 '13 at 12:26
  • @AlonsoDominguez Now I know what you mean with "removing the brackets..". Thx for the edit. – Steve Benett Aug 08 '13 at 14:40
0
JsonParser parser = new JsonParser();
JsonObject object = (JsonObject)parser.parse(yourString);

for (Map.Entry<String,JsonElement> entry : object.entrySet()) {
    JsonArray array = entry.getValue().getAsJsonArray();
    for (JsonElement elementJSON : array) {
        [...]
    }
}
biology.info
  • 3,500
  • 2
  • 28
  • 39