本文介绍了GSON嵌套类参数没有任何值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有解析字符串嵌套类的问题。这是我的JSON字符串:

  [
 {
  设备名称:DevTest中,
  StolenFlag:假的,
  BatteryLevel:2,
  LastLocalization:{
     地理:{
        CoordinateSystemId:4326,
        WellKnownText:POINT(52.403371 16.955098)
         }
      }
 },
 {
  设备名称:testdev2,
  StolenFlag:假的,
  BatteryLevel:2,
  LastLocalization:{
     地理:{
        CoordinateSystemId:4326,
        WellKnownText:POINT(16.955098 52.403371)
         }
      }
 }
]

我的类:

 类地理实现Parcelable {
    @SerializedName(CoordinateSystemId)
公众诠释CoordinateSystemId;@SerializedName(WellKnownText)
公共字符串WellKnownText;
....
}类mydevice在实现Parcelable {
    @SerializedName(设备名称)
    公共字符串设备名称;    @SerializedName(StolenFlag)
    公共布尔StolenFlag;    @SerializedName(BatteryLevel)
    公众诠释BatteryLevel;
    @SerializedName(LastLocalization)
    公共地理LastLocalization;    ...
}

和转换code:

  GSON GSON =新GSON();
            清单< mydevice在> mydev的= gson.fromJson(因此,新TypeToken<名单,LT; mydevice在>>(){
            } .getType());

由于设备的参数正确转换,我得到里面的设备地理对象空/ 0值。你有什么想法,为什么?

我收到的设备列表以及每个包含地理对象。该对象的参数为null / 0。我希望你能理解。

@EDIT @

我已经改变
公共地理LastLocalization;

公开地图<弦乐,地理> LastLocalization;

和它工作正常,但在writeToParcel方法得到了一个错误。任何想法如何解决这个问题?

  @覆盖
    公共无效writeToParcel(DEST包裹,INT标志){
        dest.writeString(设备名称);
        dest.writeByte((字节)(StolenFlag?1:0));
        dest.writeInt(BatteryLevel);
        dest.writeParcelable(LastLocalization,0);
    }
}


解决方案

  @SerializedName(LastLocalization)
公共地理LastLocalization;

这个定义告诉你期待一个名为LastLocalization一个地理类型的对象GSON,你要存储在一个名为LastLocalization成员。

您输入有对象的数组,每个包含一个名为LastLocalization一个对象,然后包含一个名为地理的对象。

当GSON遇到了JSON的LastLocalization对象,它也看到的通过明确地标注的,它应该符合你LastLocalization成员,因此它会创建一个新的地理对象,并有分配给它。它创建了一个地理对象,因为这是成员的类型 - 没有别的有意义有的。然而,没有将JSON LastLocalization对象属性的匹配任何java的地理对象的属性,所以地理只是包含用于Java对象的缺省值。

请意识到GSON不关心,如果一个JSON对象的名称是比你的Java对象不同,因为你的Java对象被明确注明为接收与不同的JSON名字的东西。

I have issue with parsing string to nested classes. This is my json string:

[
 {
  "DeviceName":"devtest",
  "StolenFlag":false,
  "BatteryLevel":2,
  "LastLocalization":{
     "Geography":{
        "CoordinateSystemId":4326,
        "WellKnownText":"POINT (52.403371 16.955098)"
         }
      }
 },
 {
  "DeviceName":"testdev2",
  "StolenFlag":false,
  "BatteryLevel":2,
  "LastLocalization":{
     "Geography":{
        "CoordinateSystemId":4326,
        "WellKnownText":"POINT (16.955098 52.403371)"
         }
      }
 }
]

My classes:

class Geography implements Parcelable{
    @SerializedName("CoordinateSystemId")
public int CoordinateSystemId;

@SerializedName("WellKnownText")
public String WellKnownText;
....
}

class MyDevice implements Parcelable{
    @SerializedName("DeviceName")
    public String DeviceName;

    @SerializedName("StolenFlag")
    public Boolean StolenFlag;

    @SerializedName("BatteryLevel")
    public int BatteryLevel;


    @SerializedName("LastLocalization")
    public Geography LastLocalization;

    ...
}

And converting code:

Gson gson = new Gson();
            List<MyDevice> MyDev = gson.fromJson(result, new TypeToken<List<MyDevice>>() {
            }.getType());

Since Device's parameters are converted correctly I get null/0 values in Geography object inside Device. Do you have any ideas why?

I receive a list with Devices and each contains Geography object. That object's parameters are null/0. I hope you understand.

@EDIT@

I have changed public Geography LastLocalization;to public Map<String, Geography> LastLocalization;

And it works fine, but got an error in writeToParcel method. Any ideas how to fix it?

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(DeviceName);
        dest.writeByte((byte) (StolenFlag ? 1 : 0));
        dest.writeInt(BatteryLevel);
        dest.writeParcelable(LastLocalization, 0);
    }
}
解决方案
@SerializedName("LastLocalization")
public Geography LastLocalization;

This definition tells Gson that you're expecting a Geography type object named LastLocalization, and you want to store that in a member called LastLocalization.

Your input has an array of objects, each of which contains an object named LastLocalization, which then contains an object called Geography.

When Gson encounters the LastLocalization object in the JSON, it also sees by explicity annotation that it should match your LastLocalization member, so it creates a new Geography object and assigns it there. It creates a Geography object because that's the type of the member - nothing else makes sense there. However, none of the JSON LastLocalization object properties match any of the java Geography object properties, so Geography just contains the default values for a Java object.

Please realize that Gson doesn't care if the name of a JSON object is different than your Java object because your Java object was explicitly annotated to receive something with a different JSON name.

这篇关于GSON嵌套类参数没有任何值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 07:10