使用Gson将JSON反序列化为非静态嵌套类 [英] Deserializing JSON to non-static nested classes using Gson
问题描述
根据这 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时,我总是需要使它们 Gson也可以反序列化静态嵌套类,但是,Gson不能 无论如何,如果它真的可行反序列化为一个非静态的内部类,你的问题将是... 首先,您将解析JSON与您的类 这应该是工作的,因为类 如果您的第一个解析实际上正在工作,如果您想要访问 如果使用类 According to this Gson can deserialize to inner classes. I have the next fragment of JSON string: I'm using the next class: and trying to parse the my JSON string: But I get this error: Could you please tell me where the mistake is? When I used Gson with nested classes I always needed to make them "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 which should be working, since the class If your first parsing is actually working, if you then want to access the If the parsing with the class 这篇关于使用Gson将JSON反序列化为非静态嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! code>工作...在你的链接中,他们说这不是必须的,但是在
$ b $ b
自动反序列化纯内部类,因为它们的无参数
构造函数也需要对包含Object的引用
在反序列化时不可用。您可以通过使内部类为静态或通过提供一个
自定义InstanceCreator。
Tweet
with:
Tweet tweet = gson.fromJson(string,Tweet.class);
Tweet
匹配 JSON响应。然而,你正试图解析相同的JSON响应,类 Coordinates
,这显然不匹配JSON响应。另外,根本没有意义解析相同的响应两次!
坐标
对象,请执行以下操作:
Tweet.Coordinates tweetCoordinates = tweet.getCordinates();
Tweet
解析是不工作,尝试使内部类静态
,如果这也不起作用,请评论,我会尝试找到另一个解决方案... p> ...
"coordinates": {
"coordinates": [106.80552006,-6.22016938],
"type": "Point",
}
...
public class Tweet {
public Coordinates coordinates = new Coordinates();
public class Coordinates {
public double[] coordinates;
}
}
Tweet tweet = gson.fromJson(string, Tweet.class);
Tweet.Coordinates tweetCoordinates = gson.fromJson(string, Tweet.Coordinates.class);
Expected BEGIN_ARRAY but was BEGIN_OBJECT
static
to work... In your link they say that it's not necessary, but in Gson documentation it's clearly said:
Tweet
with:Tweet tweet = gson.fromJson(string, Tweet.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! Coordinates
object, just do:Tweet.Coordinates tweetCoordinates = tweet.getCordinates();
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...