0

JSON Response String:

{
    "1": {
        "registered_name": "Manchester Cars",
        "search_tag": null,
        "town": "Manchester",
        "distance": "1.03782874345779",
        "capacity": 4,
        "first_address_line": null,
        "price": "165.00",
        "cost_per_person": "165.00",
        "trip_direction": 1,
        "mco_id": "826",
        "tag": ""
    },
    "2": {
        "registered_name": "Avon Cars",
        "search_tag": null,
        "town": "Solihull",
        "distance": "3.39530396461487",
        "capacity": 4,
        "first_address_line": null,
        "price": "140.82",
        "cost_per_person": "140.82",
        "trip_direction": 1,
        "mco_id": "670",
        "tag": ""
    },
    "3": {
        "registered_name": "BBS Executive",
        "search_tag": null,
        "town": "Birmingham",
        "distance": "11.7371127605438",
        "capacity": 4,
        "first_address_line": null,
        "price": "186.17",
        "cost_per_person": "186.17",
        "trip_direction": 1,
        "mco_id": "615",
        "tag": ""
    },
    "4": {
        "registered_name": "Sky Cars",
        "search_tag": null,
        "town": "Birmingham",
        "distance": "14.4878869056702",
        "capacity": 4,
        "first_address_line": null,
        "price": "179.13",
        "cost_per_person": "179.13",
        "trip_direction": 1,
        "mco_id": "822",
        "tag": ""
    },
    "": {
        "registered_name": "Eco Cars Manchester",
        "search_tag": null,
        "town": "Stockport",
        "distance": "9.5043933391571",
        "capacity": 4,
        "first_address_line": null,
        "price": "155.00",
        "cost_per_person": "155.00",
        "trip_direction": 1,
        "mco_id": "774",
        "tag": ""
    }
}

I want to convert this json response, i am using following GSON code but failed to convert,

Tried Solution 1

Gson gson = new Gson();         
Item[] itemList = gson.fromJson(jstring, Item[].class);    
Type collectionType = new TypeToken<Collection<Item>>(){}.getType();
List<Item> enums =  gson.fromJson(jstring, collectionType);

Tried Solution 2

Gson gson = new Gson();         
Item[] itemList = gson.fromJson(jstring, Item[].class);

Where jstring is json response printed above.

Failed: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

PCS
  • 211
  • 3
  • 11
  • Refer the following Converting [json to gson](http://stackoverflow.com/questions/2779251/convert-json-to-hashmap-using-gson-in-java) – Harish Oct 15 '12 at 12:25

2 Answers2

2

Your JSON Response is not a JSON Array. It is a JSON Object. That is the cause of your error. See www.json.org

Furkan
  • 683
  • 2
  • 10
  • 26
  • Yes, but still i want to convert my json string how can i? – PCS Oct 15 '12 at 12:18
  • your response is a json object containing json objects (but not as an array). So, I see two way of doing this. First, if the number of inner json objects (items) does not change, define a class with that number of items (not as an array, each as a different attribute). Or preprocess your string and change first character to [ and last character to ]. I know both are ugly way of solving the problem but I don't see any other way unless you cannot change the response sender. Because the problem is with the response. – Furkan Oct 15 '12 at 12:24
  • No inner classes are going to change as per the requests, means they may return sometimes 2 or 3 or 6 objects. Actually server cant modify the response because the Iphone app also uses the same request(they have Dictionary to convert this json response so working fine in iphone development). – PCS Oct 15 '12 at 12:30
  • just preprocess your string then before sending it to gson. Change the first and the last character. – Furkan Oct 15 '12 at 12:31
1

You can try using Genson http://code.google.com/p/genson/. The following code should solve your problem.

Map<String, Item> itemsMap = new Genson().deserialize(jsonString, new GenericType<Map<String, Item>>() {});
// get all the items independently from their keys
Collection<Items> items = itemsMap.values();
eugen
  • 5,856
  • 2
  • 29
  • 26