Android Gson类型转换错误解决 com.google.gson.internal.LinkedTreeMap

com.google.gson.internal.LinkedTreeMap不能转换到我的类(com.google.gson.internal.LinkedTreeMap cannot be cast to my class)

I have some problems with getting my object from a JSON string.

I got the class Product

public class Product {
    private String mBarcode;
    private String mName;
    private String mPrice;

    public Product(String barcode, String name, String price) {
        mBarcode = barcode;
        mName = name;
        mPrice = price;
    }

    public int getBarcode() {
        return Integer.parseInt(mBarcode);
    }

    public String getName() {
        return mName;
    }

    public double getPrice() {
        return Double.parseDouble(mPrice);
    }
}    

From my server I get an ArrayList<Product> in JSON String representation. For example:

[{"mBarcode":"123","mName":"Apfel","mPrice":"2.7"},
{"mBarcode":"456","mName":"Pfirsich","mPrice":"1.1111"},
{"mBarcode":"89325982","mName":"Birne","mPrice":"1.5555"}] 

This String is generated like this:

public static <T> String arrayToString(ArrayList<T> list) {
    Gson g = new Gson();
    return g.toJson(list);
}

To get my Object back I use this function:

public static <T> ArrayList<T> stringToArray(String s) {
    Gson g = new Gson();
    Type listType = new TypeToken<ArrayList<T>>(){}.getType();
    ArrayList<T> list = g.fromJson(s, listType);
    return list;
}

But when calling

String name = Util.stringToArray(message).get(i).getName();

I get the error com.google.gson.internal.LinkedTreeMap cannot be cast to object.Product

What am I doing wrong? It looks like it created a List of LinkedTreeMaps but how do i convert those into my Product Object?

解决方案

In my opinion, due to type erasure, the parser can't fetch the real type T at runtime. One workaround would be to provide the class type as parameter to the method.

Something like this works, there are certainly other possible workarounds but I find this one very clear and concise.

public static <T> List<T> stringToArray(String s, Class<T[]> clazz) {
    T[] arr = new Gson().fromJson(s, clazz);
    return Arrays.asList(arr); //or return Arrays.asList(new Gson().fromJson(s, clazz)); for a one-liner
}

And call it like:

String name = stringToArray(message, Product[].class).get(0).getName();

翻译:
问 题

从JSON字串取得我的物件时发生问题。



我有产品



  public class Product {
 private String mBarcode; 
 private String mName; 
 private String mPrice; 
 
 public Product(String barcode,String name,String price){
 mBarcode = barcode; 
 mName = name; 
 mPrice = price; 
} 
 
 public int getBarcode(){
 return Integer.parseInt(mBarcode); 
} 
 
 public String getName(){
 return mName; 
} 
 
 public double getPrice(){
 return Double.parseDouble(mPrice); 
} 
} 
  

从我的服务器,我得到一个 ArrayList< Product> in JSON字符串表示形式。例如:


  [{“mBarcode”:“123”,“mName”:“Apfel”,“mPrice”:“ “mBarcode”:“89325982”,“mName”:“ :“Birne”,“mPrice”:“1.5555”}] 
  

  public static< T> String arrayToString(ArrayList< T> list){
 Gson g = new Gson(); 
 return g.toJson(list); 
} 
  


要使我的对象回来,我使用这个函数:



  public static< T> ArrayList< T> stringToArray(String s){
 Gson g = new Gson(); 
类型listType = new TypeToken< ArrayList< T>>(){}。getType(); 
 ArrayList< T> list = g.fromJson(s,listType); 
 return list; 
} 
  

但是当调

  String name = Util.stringToArray(message).get(i).getName(); 
  

我得到错误
com.google.gson.internal.LinkedTreeMap不能被投射到对象。产品

我做错了什么?看起来它创建了一个LinkedTreeMaps列表,但是如何将它转换为我的产品对象?

解决方案

为了进行类型擦除,解析器不能在运行时获取真实类型T.一种解决方法是提供类类型作为方法的参数。


这样的工作原理,当然还有其他可能的解决方法,但我觉得这个很清楚简洁。


  public static< T>列表< T> stringToArray(String s,Class< T []> clazz){
 T [] arr = new Gson()。fromJson(s,clazz); 
 return Arrays.asList(arr); //或者返回Arrays.asList(new Gson()。fromJson(s,clazz)); 
} 
  

并像这样调用:

 字符串名称= stringToArray(消息,Product []类).get(0).getName 
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值