2012-10-15 98 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