【问题标题】:java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 62java.lang.IllegalStateException:应为 BEGIN_OBJECT,但在第 1 行第 62 列为 STRING
【发布时间】:2013-12-20 23:44:24
【问题描述】:

我遇到了关于将 GSON json 转换为 Java 的问题。我在这里查找了很多帖子,但我找不到我的问题的解决方案。所以我在这里列出我的问题。我正在尝试获取数据,在 json 中有一个 Map 但我无法检索数据。在我的日志中,我可以看到只有这么多数据会出现然后它抛出异常。有人请指导我通过。非常感谢!

这是我从我正在开发的 Android 应用程序中点击 URL 的 Json 数据

编辑

{
"success" : true,
"messages" : {
    "success" : [
        "SEARCH_QUERY_SUCCESS"
    ]
},
"session" : {
    "id" : "cn694ivr8bmqnrveh9n8841oh7",
    "expire" : "",
    "YII_CSRF_TOKEN" : "4fa0ae103b547836241f5278311839b407050919"
},
"metadata" : {
    "product_count" : "4458",
    "category_ids" : "3",
    "results" : [{
            "id" : "105089",
            "data" : {
                "sku" : "MA851AA10ZLX",
                "name" : "Alexa Mid Rise Super Skinny Leg",
                "new-product" : false,
                "url" : "http:\/\/theiconic.bugfoot.de\/mobile-api\/Alexa-Mid-Rise-Super-Skinny-Leg-105089.html",
                "simples" : {
                    "MA851AA10ZLX-406437" : {},
                    "MA851AA10ZLX-406438" : {},
                    "MA851AA10ZLX-406439" : {},
                    "MA851AA10ZLX-406440" : {},
                    "MA851AA10ZLX-406441" : {},
                    "MA851AA10ZLX-406442" : {},
                    "MA851AA10ZLX-406443" : {},
                    "MA851AA10ZLX-406444" : {
                        "meta" : {
                            "sku" : "MA851AA10ZLX-406444",
                            "price" : "149.99",
                            "caching_hash" : "78ddaaf930f8bd0e0bf595c25643683d",
                            "shipment_cost_item" : "0.00",
                            "shipment_cost_order" : "0.00",
                            "tax_percent" : "10.00",
                            "quantity" : "2",
                            "cost" : "64.09",
                            "size_brand" : "W31\/L34",
                            "size" : "W31\/L34",
                            "size_position" : "200",
                            "3hours_shipment_available" : true,
                            "estimated_delivery" : "",
                            "estimated_delivery_position" : ""
                        },
                        "attributes" : {
                            "sort_order" : "0",
                            "size" : "W31\/L34"
                        }
                    }
                }
            }
        }
    ]
}
}

编辑

这是我用来解析的网络类

String jsonString = null;

    try {
        HttpGet httppost = new HttpGet(URL);
        HttpClient httpClient = new DefaultHttpClient();
        if (httpClient != null) {
            HttpResponse response = httpClient.execute(httppost);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            response.getEntity().getContent(), "UTF-8"));
            jsonString = reader.readLine();
        }

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(jsonString);

    if (jsonString != null) {
        Bean obj=new Gson().fromJson(jsonString, Bean.class);
        Message msg = new Message();
        msg.obj = obj;
        responseHandler.sendMessage(msg);
        //obj can be sent to a handler 
    }
}
}

这是一个包含 Map 的 Simples Bean 类

public class Simples {
private Map<String, KeyMap> keyMap;
public Map<String, KeyMap> getKeyMap() {return keyMap;}
public void setKeyMap(Map<String, KeyMap> keyMap) {this.keyMap = keyMap;}}

这是我的 KeyMap Bean 类

public class KeyMap {

private Meta meta;
private Attributes attributes;

public Meta getMeta() {
    return meta;
}

public void setMeta(Meta meta) {
    this.meta = meta;
}

public Attributes getAttributes() {
    return attributes;
}

public void setAttributes(Attributes attributes) {
    this.attributes = attributes;
}
}

【问题讨论】:

  • 您没有在问题中提供相关信息
  • 您应该至少发布解析 JSON 结构的代码部分。
  • 发布你的模型类
  • @karansingh1487 首先你的 json 格式无效,另外就是你没有发布你的模型类
  • 您好,我会在晚上回家时发布它们,抱歉给您带来不便.....抱歉耽搁了,请多多包涵

标签: java android json map gson


【解决方案1】:

实际上你得到了错误BEGIN_OBJECT but was STRING,因为 gson 期望的是对象而不是字符串,它也是 json 中的一个对象,你错误地将你的类映射到 Bean 类中,并且你没有发布 Bean,简单类粘合代码在这里。

你的 JSON 模型类可以像这样映射

public class ProductInfo {
    private boolean success;
    private Map<String, String[]> messages;
    private SessionData session;
    private MetaData metadata;
}
public class SessionData {
    private String id;
    private String expire;
    private String YII_CSRF_TOKEN;
}
public class MetaData {
    private String product_count;
    private String category_ids;
    private List<Result> results;
}
public class Result {
    private String id;
    private Data data;
}
public class Data {
    private String sku;
    private String name;
    @SerializedName(value = "new-product")
    private String newProduct;
    private String url;
    Map<String, KeyMap> simples;
}
public class KeyMap {
    private Meta meta;
    private Attributes attributes;
}

public class Meta {
    private String sku;;
    private String price;
    private String caching_hash;
    private String shipment_cost_item;
    private String shipment_cost_order;
    private String tax_percent;
    private String quantity;
    private String cost;
    private String size_brand;
    private String size;
    private String size_position;
    @SerializedName(value = "3hours_shipment_available")
    private String hours_shipment_available;
    private String estimated_delivery;
    private String estimated_delivery_positio;
}
public class Attributes {
    private String sort_order;
    private String size;
}

终于反序列化了

ProductInfo productInfo = gson.fromJson(reader, ProductInfo.class);

享受:)

【讨论】:

  • 感谢 fr 的回复,但我的 Bean 类与您的 ProductInfo 类相同,只是我没有将 Map 用于消息的一个更改是我遇到问题的原因?
  • 上面也提到了Simples类。
  • 我的简单 bean 即将为 Null,尽管我没有得到任何异常
猜你喜欢
  • 2022-01-08
  • 2018-07-28
  • 1970-01-01
  • 2023-01-01
  • 2015-10-04
  • 2019-04-28
  • 2019-11-27
  • 2020-04-10
  • 2021-09-03
相关资源
最近更新 更多