我收到以下错误:
java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 29 path $.entree[0].photo
这对我来说没有意义,因为我的 json 响应应该构造如下:
import com.google.gson.annotations.SerializedName;
public class Entree {
@SerializedName("id")
public int id;
@SerializedName("photo")
public Entree.Photo photo;
public class Photo {
@SerializedName("url")
public String url;
@SerializedName("web")
public Entree.Photo.Web web;
@SerializedName("mobile")
public Entree.Photo.Mobile mobile;
public class Web {
@SerializedName("url")
public String url;
}
public class Mobile {
@SerializedName("url")
public String url;
}
}
}
JSON 数据:
"entrees": [
{
"id": 32,
"photo":
{
"url": "4c312e9aed37a59319096a03_1.jpg",
"web": {
"url": "web_4c312e9aed37a59319096a03_1.jpg"
},
"mobile": {
"url": "mobile_4c312e9aed37a59319096a03_1.jpg"
}
}
},
...
你明白为什么 Photo 在我的类中是一个对象而不是字符串吗?那我做错了什么?
最佳答案
尝试用static
标记内部类
在 Gson document ,它说
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.
关于java - 需要一个字符串,但在第 1 行第 29 列路径处为 BEGIN_OBJECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48255382/