2012-10-15 96 views
0

JSON响应字符串:阅读GSON从JSON字符串

{ 
    "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": "" 
    } 
} 

我想这个JSON响应转换中,我使用以下GSON的代码,但无法转换,

尝试解决方案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); 

试过的解决方案2

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

其中jstring是json响应打印在上面。

失败: java.lang.IllegalStateException:预期BEGIN_ARRAY但BEGIN_OBJECT位于第1行第2列

+0

请参考下转换[JSON来GSON](http://stackoverflow.com/questions/2779251/convert-json-to-hashmap-using-gson-in-java) – Harish

回答

2

你的JSON响应是不是一个JSON阵列。它是一个JSON对象。这是你错误的原因。请参阅www.json.org

+0

是,但我仍然想要转换我的JSON字符串我怎么能? – PCS

+0

你的回应是一个包含json对象(但不是数组)的json对象。所以,我看到了这样做的两种方式。首先,如果内部json对象(项目)的数量没有变化,请定义一个具有该项目数量的类(不是数组,每个都是不同的属性)。或者预处理您的字符串并将第一个字符更改为[并将最后一个字符更改为]。我知道这两个都是解决问题的难看方式,但除非您无法更改回复发件人,否则我不会看到任何其他方式。因为问题与回应有关。 – Furkan

+0

没有内部类将根据请求而改变,意味着它们有时可能会返回2或3或6个对象。实际上服务器无法修改响应,因为Iphone应用程序也使用相同的请求(他们有Dictionary来转换此JSON响应,因此在iPhone开发中工作正常)。 – PCS

1

您可以尝试使用Genson http://code.google.com/p/genson/。 以下代码应该可以解决您的问题。

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(); 
+0

是的,谢谢,它的工作。 – PCS