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 

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

  1. Android之奔溃提示com.google.gson.internal.LinkedTreeMap cannot be cast to java.util.HashMap

    1 问题 Android端获取服务端的数据然后我直接把数据转hashMap提示错误如下, com.google.gson.internal.LinkedTreeMap cannot be cast t ...

  2. Caused by: android.os.NetworkOnMainThreadException错误解决办法

    错误Caused by: android.os.NetworkOnMainThreadException :查了下原因上在4.0之后在主线程里面执行Http请求都会报这个错,大概是怕Http请求时间太 ...

  3. system.err android.os.NetworkOnmainThreadException 错误解决办法

    学习HttpClient是,出现了这个异常,原因暂时还不知道. 解决办法:修改 AndroidManifest.xml <uses-sdkandroid:minSdkVersion=" ...

  4. android.database.cursorindexoutofboundsexception错误解决 及获取某行某列信息

    1 /** 2 * 获取某行某列信息 3 * @param info 4 * @param column 5 * @return 6 */ 7 public static int getIntValu ...

  5. android gson工具包,Android Study 之 彻底解决 Gson解析 异常

    > LZ-Say:大风呼呼的刮,就好像妖精要来的前奏一样~ > > 前言 开发过程中,App常用的接收参数的时候,几乎大部分都是以json格式为主,那么有关解析json的方式有很多, ...

  6. Gson:我爸是 Google

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 01.前世今生 我叫 Gson,是一款开源的 Java 库 ...

  7. Google Gson

    JackSon简单用法        FastJson简单用法 一.Gson介绍 1.什么是Gson Gson 是 Google 开发的Java API,是一个简单基于Java的开源库,可以相互转换J ...

  8. linux编译安装的报错,linux编译安装时常见错误解决办法

    This article is post on https://coderwall.com/p/ggmpfa 原文链接:http://www.bkjia.com/PHPjc/1008013.html ...

  9. adb avd install 失败_Android 模拟器(emulator-5554...)出现错误解决办法

    Android 模拟器 出现错误解决办法: 1.Unable to get view server version from device exlipse下编写好android应用程序时候,右键项目 ...

最新文章

  1. WMI技术介绍和应用——Event Consumer Provider
  2. vue启动时报错,node-modules下xxx缺失
  3. 批量过滤删除AD中的对象
  4. 虚拟机在安装svn时 认证密码一直报错_研路同行|安装虚拟机到安装GAMIT教程步骤汇总...
  5. hadoop启动页面_轻松搞定Windows下的Hadoop环境安装
  6. 提高生产力:文件和IO操作(ApacheCommonsIO-汉化分享)
  7. Windows XP Embedded 上手指南
  8. iview日期控件,双向绑定日期格式
  9. Linux shell中比较操作符“==”与“-eq”对比
  10. Docker 开发环境正在崩坏!
  11. 【文本识别】CRNN
  12. 中科大网上财务报销填写流程
  13. 使用canvas实现贪吃蛇
  14. 易语言单窗口单进程单IP技术
  15. 盘点:当今十大备份应用软件(转)
  16. [Echarts可视化] 一.入门篇之简单绘制中国地图和贵州地区
  17. OSChina 娱乐弹弹弹——凉风有信,秋月无边
  18. 单片机毕业设计 stm32火灾报警系统
  19. Hibernate查询多个表的数据的方法
  20. 基于JAVA大学生兼职网站计算机毕业设计源码+系统+数据库+lw文档+部署

热门文章

  1. Linux多进程多线程编程笔记
  2. 五角大楼怒赞Windows 10:不用都不好意思跟别人打招呼
  3. macbook睡眠还掉电?阻止Mac休眠异常耗电的方法
  4. Quartz定时任务的使用
  5. 我叔叔愿意从事计算机工作英语翻译,我的叔叔作文范文
  6. java毕业设计springboot生鲜超市商城管理系统研究与实现
  7. L11-Python中的高阶函数的使用
  8. 什么?java也能弹钢琴?
  9. jQuery 实现模糊查询
  10. 小型web服务器的编写