【问题标题】:Reading Gson from json string从 json 字符串中读取 Gson
【发布时间】:2012-10-15 11:57:26
【问题描述】:

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,但在第 1 行第 2 列是 BEGIN_OBJECT

【问题讨论】:

标签: android json gson


【解决方案1】:

您的 JSON 响应不是 JSON 数组。它是一个 JSON 对象。这就是你的错误的原因。见www.json.org

【讨论】:

  • 是的,但我仍然想转换我的 json 字符串,我该怎么做?
  • 您的响应是一个包含 json 对象(但不是数组)的 json 对象。所以,我看到了两种方法。首先,如果内部 json 对象(项目)的数量没有改变,则使用该数量的项目定义一个类(不是作为数组,每个作为不同的属性)。或者预处理您的字符串并将第一个字符更改为 [ 并将最后一个字符更改为 ]。我知道两者都是解决问题的丑陋方式,但除非您无法更改响应发件人,否则我看不到任何其他方式。因为问题在于响应。
  • 没有内部类会根据请求进行更改,这意味着它们有时可能会返回 2 个或 3 个或 6 个对象。实际上服务器无法修改响应,因为 Iphone 应用程序也使用相同的请求(他们有字典来转换此 json 响应,因此在 iphone 开发中工作正常)。
  • 只需预处理您的字符串,然后再将其发送到 gson。更改第一个和最后一个字符。
【解决方案2】:

您可以尝试使用 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多