java - 需要一个字符串,但在第 1 行第 29 列路径处为 BEGIN_OBJECT

标签 java android json

我收到以下错误:

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/

相关文章:

java - Android 中的网络

java - 像 Gmail 应用程序一样的 Android Studio 抽屉导航

java - FileInputStream 是否没有缓冲以及为什么 BufferedInputStream 更快?

java - 写入 SD 卡 "java.io.FileNotFoundException: Permission denied"

javascript - Angular 不解析 json

java - 如何使用最佳方式检索 Json 文件

javascript - jquery 和 ajax。如何检索该字段的数据并返回到正确的复选框

java - 嵌入式 Groovy : How to use static type checking with external variables?

java - Android 平台上是否有一个事件处理程序可以代替物理按钮按下?

java - expected=JSONException.class 没有捕捉到