使用Gson将JSON反序列化为非静态嵌套类 [英] Deserializing JSON to non-static nested classes using Gson

查看:113
本文介绍了使用Gson将JSON反序列化为非静态嵌套类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Gson可以反序列化为内部类。我有JSON字符串的下一个片段:

  ... 
坐标:{
坐标:[106.80552006,-6.22016938],
type:Point,
}
...

我正在使用下一个课程:

  public class Tweet {
公共坐标坐标=新坐标();

公共类坐标{
public double []坐标;


并试图解析我的JSON字符串:

  Tweet tweet = gson.fromJson(string,Tweet.class); 
Tweet.Coordinates tweetCoordinates = gson.fromJson(string,Tweet.Coordinates.class);

但是我得到这个错误:

 预计BEGIN_ARRAY,但是BEGIN_OBJECT 

请您告诉我错误是什么?

解决方案

当我使用嵌套类的Gson时,我总是需要使它们 code>工作...在你的链接中,他们说这不是必须的,但是在

$ b $ b

Gson也可以反序列化静态嵌套类,但是,Gson不能
自动反序列化纯内部类,因为它们的无参数
构造函数也需要对包含Object的引用
在反序列化时不可用。您可以通过使内部类为静态或通过提供一个
自定义InstanceCreator。







无论如何,如果它真的可行反序列化为一个非静态的内部类,你的问题将是...



首先,您将解析JSON与您的类 Tweet with:

  Tweet tweet = gson.fromJson(string,Tweet.class); 

这应该是工作的,因为类 Tweet 匹配 JSON响应。然而,你正试图解析相同的JSON响应,类 Coordinates ,这显然不匹配JSON响应。另外,根本没有意义解析相同的响应两次!



如果您的第一个解析实际上正在工作,如果您想要访问坐标对象,请执行以下操作:

  Tweet.Coordinates tweetCoordinates = tweet.getCordinates(); 

如果使用类 Tweet 解析是不工作,尝试使内部类静态,如果这也不起作用,请评论,我会尝试找到另一个解决方案... p>

According to this Gson can deserialize to inner classes. I have the next fragment of JSON string:

...
"coordinates": {
    "coordinates": [106.80552006,-6.22016938],
    "type": "Point",
}
...

I'm using the next class:

public class Tweet {
  public Coordinates coordinates = new Coordinates();

  public class Coordinates {
    public double[] coordinates;
  }
}

and trying to parse the my JSON string:

Tweet tweet = gson.fromJson(string, Tweet.class);
Tweet.Coordinates tweetCoordinates = gson.fromJson(string, Tweet.Coordinates.class);

But I get this error:

Expected BEGIN_ARRAY but was BEGIN_OBJECT

Could you please tell me where the mistake is?

解决方案

When I used Gson with nested classes I always needed to make them static to work... In your link they say that it's not necessary, but in Gson documentation it's clearly said:

"Gson can also deserialize static nested classes. However, Gson can not automatically deserialize the pure inner classes since their no-args constructor also need a reference to the containing Object which is not available at the time of deserialization. You can address this problem by either making the inner class static or by providing a custom InstanceCreator for it."


Anyway, if it's actually possible deserialize to a non-static inner class, your problem would be that...

First you are parsing the JSON with your class Tweet with:

Tweet tweet = gson.fromJson(string, Tweet.class);

which should be working, since the class Tweet matches the JSON response. However, then you are trying to parse the same JSON response with the class Coordinates, which obviously doesn't match the JSON response... moreover it makes no sense at all to parse the same response twice!

If your first parsing is actually working, if you then want to access the Coordinates object, just do:

Tweet.Coordinates tweetCoordinates = tweet.getCordinates();

If the parsing with the class Tweet is not working either, try to make the inner class static, and if that doesn't work either, please comment and I'll try to find another solution...

这篇关于使用Gson将JSON反序列化为非静态嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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