google gson LinkedTreeMap类转换为myclass [英] google gson LinkedTreeMap class cast to myclass

查看:2750
本文介绍了google gson LinkedTreeMap类转换为myclass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题以前就被问过了。由于我在java和android的新手技能。我无法解决这个问题超过一个星期。

我的一位朋友和我正在开发一个android项目,这里有一些事情。

这件事最奇怪的部分是,它只是在我从Google Play商店下载并测试它时才发生。不是从本地android studio安装或调试模式。



这里有什么问题,或者这个返回的列表完全错误?
我的朋友说服这段代码正确地返回,但是从游戏商店安装它始终是一个错误。



请建议我应该在哪里继续挖掘?

  @Override 
public void promiseMethod(JSONObject object){$ b $ if(object!= null){

if(object.has(DO_SERVICES)){
vehicleDetails = new ArrayList < Object []> (1);
列表<字符串> vehicleNoList = new ArrayList<字符串> (1);
列表<字符串> serviceList = new ArrayList<字符串> (1);
尝试{
Gson gson = new Gson();
JSONObject jsonObj = new JSONObject(object.get(DO_SERVICES)
.toString());
servDto = gson.fromJson(jsonObj.toString(),
ServiceDto.class);


if(servDto.getServiceDto()instanceof List){

List< DoServiceDto> doServiceList = servDto.getServiceDto();

例外是


java.lang.ClassCastException:com.auogle.gson.internal.LinkedTreeMap无法在com.gaurage.user.User_Test_Main.promiseMethod(未知源代码)中转换为com.gaurage.dto.DoServiceDto



解决方案

序列化和反序列化泛型

当你调用toJson(obj)时,Gson调用obj.getClass()来获取要序列化的字段的信息。同样,您通常可以在fromJson(json,MyClass.class)方法中传递MyClass.class对象。如果对象是非泛型类型,这工作正常。但是,如果对象是泛型类型,那么由于Java类型擦除,泛型类型信息会丢失。这里有一个例子说明了这一点:

  class Foo< T> {T值;} 
Gson gson = new Gson();
Foo< Bar> foo = new Foo< Bar>();
gson.toJson(foo); //不能正确序列化foo.value
gson.fromJson(json,foo.getClass()); //无法将foo.value反序列化为Bar

上面的代码无法将值解释为类型Bar,因为Gson调用list.getClass()来获得它的类信息,但是这个方法返回一个原始类Foo.class。这意味着Gson无法知道这是Foo类型的对象,而不仅仅是普通的Foo。

您可以通过为泛型指定正确的参数化类型来解决此问题。您可以通过使用TypeToken类来完成此操作。

 键入fooType = new TypeToken< Foo< Bar>>(){} .getType(); 
gson.toJson(foo,fooType);
gson.fromJson(json,fooType);

我有Parent类,它是子类,其中一些列表类型在其中。像这个父类一样,我有30个文件。我像这样解决了它。

  Gson gson = new Gson(); 
JSONObject jsonObj = new JSONObject(object.get(DO_SERVICES).toString());
Type type = new TypeToken< MyDto>(){} .getType();
servDto = gson.fromJson(jsonObj.toString(),type);

最重要的是,我无法在Android的本地测试中重现此错误工作室。此问题只会弹出,当我生成已签名的apk并将应用程序发布到PlayStore中时,应用程序会停止,并且报告显示无法将LinkedTreeMap投射到myclass。

我很难在我的本地测试(包括调试模式)中重现相同的结果。


I knew this question has been asked before. Due to my novice skill in java and android. I can't resolve this issue for more than a week.

One of my friend and i developing an android project were there are a couple of things like this.

The most weird part of this things is, it's happening only from when i download and test it from Google play store. Not from local android studio installation or debug mode.

What could be the problem here, or this returning list which is totally wrong ? My friend convincing that this code returns correctly but from play store installation it's always an error.

Please suggest me where should i keep digging?

@Override
public void promiseMethod(JSONObject object) {
    if (object != null) {

        if (object.has(DO_SERVICES)) {
            vehicleDetails = new ArrayList < Object[] > (1);
            List < String > vehicleNoList = new ArrayList < String > (1);
            List < String > serviceList = new ArrayList < String > (1);
            try {
                Gson gson = new Gson();
                JSONObject jsonObj = new JSONObject(object.get(DO_SERVICES)
                    .toString());
                servDto = gson.fromJson(jsonObj.toString(),
                    ServiceDto.class);


                if (servDto.getServiceDto() instanceof List) {

                    List < DoServiceDto > doServiceList = servDto.getServiceDto();

Exception is

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.gaurage.dto.DoServiceDto at com.gaurage.user.User_Test_Main.promiseMethod(Unknown Source)

解决方案

Serializing and Deserializing Generic Types

When you call toJson(obj), Gson calls obj.getClass() to get information on the fields to serialize. Similarly, you can typically pass MyClass.class object in the fromJson(json, MyClass.class) method. This works fine if the object is a non-generic type. However, if the object is of a generic type, then the Generic type information is lost because of Java Type Erasure. Here is an example illustrating the point:

class Foo<T> {  T value;}
Gson gson = new Gson();
Foo<Bar> foo = new Foo<Bar>();
gson.toJson(foo); // May not serialize foo.value correctly
gson.fromJson(json, foo.getClass()); // Fails to deserialize foo.value as Bar

The above code fails to interpret value as type Bar because Gson invokes list.getClass() to get its class information, but this method returns a raw class, Foo.class. This means that Gson has no way of knowing that this is an object of type Foo, and not just plain Foo.

You can solve this problem by specifying the correct parameterized type for your generic type. You can do this by using the TypeToken class.

Type fooType = new TypeToken<Foo<Bar>>() {}.getType();    
gson.toJson(foo, fooType);
gson.fromJson(json, fooType);

I have Parent class and it's child class some of them having List types in it. Like this parent class i have 30 files. I solved it like this.

Gson gson = new Gson();
JSONObject jsonObj = new JSONObject(object.get(DO_SERVICES).toString());
Type type = new TypeToken<MyDto>() {}.getType();
servDto = gson.fromJson(jsonObj.toString(),type);

The most important thing is, I can't reproduce this error in local testing from Android studio. This problem pops up only, When i generate signed apk and publish app into PlayStore were the app stops, and the report says Cannot cast LinkedTreeMap to myclass.

It was hard for me to reproduce the same result in my local testing (includes Debug mode).

这篇关于google gson LinkedTreeMap类转换为myclass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆