当前位置: 首页 > 面试题库 >

无法解析JSON响应对象

程和畅
2023-03-14
问题内容

我正在尝试从FlickR解析JSON响应,但是遇到了困难。我已经进行了一些测试,并且收到200的响应代码,并且能够使用日志记录拦截器查看实际的JSON。

但是,当我尝试访问JSON对象时,我收到了一个空指针。我觉得这与我的映射/ JSON到POJO有关:

**Model:**

相片:

public class Photos {

@SerializedName("page")
@Expose
private int page;
@SerializedName("pages")
@Expose
private int pages;
@SerializedName("perpage")
@Expose
private int perpage;
@SerializedName("total")
@Expose
private String total;
@SerializedName("photo")
@Expose
private List<Photo> photo = new ArrayList<Photo>();

/**
 *
 * @return
 *     The page
 */
public int getPage() {
    return page;
}

/**
 *
 * @param page
 *     The page
 */
public void setPage(int page) {
    this.page = page;
}

/**
 *
 * @return
 *     The pages
 */
public int getPages() {
    return pages;
}

/**
 *
 * @param pages
 *     The pages
 */
public void setPages(int pages) {
    this.pages = pages;
}

/**
 *
 * @return
 *     The perpage
 */
public int getPerpage() {
    return perpage;
}

/**
 *
 * @param perpage
 *     The perpage
 */
public void setPerpage(int perpage) {
    this.perpage = perpage;
}

/**
 *
 * @return
 *     The total
 */
public String getTotal() {
    return total;
}

/**
 *
 * @param total
 *     The total
 */
public void setTotal(String total) {
    this.total = total;
}

/**
 *
 * @return
 *     The photo
 */
public List<Photo> getPhoto() {
    return photo;
}

/**
 *
 * @param photo
 *     The photo
 */
public void setPhoto(List<Photo> photo) {
    this.photo = photo;
  }

}

照片:

public class Photo {

@SerializedName("id")
@Expose
private String id;
@SerializedName("owner")
@Expose
private String owner;
@SerializedName("secret")
@Expose
private String secret;
@SerializedName("server")
@Expose
private String server;
@SerializedName("farm")
@Expose
private int farm;
@SerializedName("title")
@Expose
private String title;
@SerializedName("ispublic")
@Expose
private int ispublic;
@SerializedName("isfriend")
@Expose
private int isfriend;
@SerializedName("isfamily")
@Expose
private int isfamily;
@SerializedName("url_m")
@Expose
private String urlM;
@SerializedName("height_m")
@Expose
private String heightM;
@SerializedName("width_m")
@Expose
private String widthM;

/**
 * @return The id
 */
public String getId() {
    return id;
}

/**
 * @param id The id
 */
public void setId(String id) {
    this.id = id;
}

/**
 * @return The owner
 */
public String getOwner() {
    return owner;
}

/**
 * @param owner The owner
 */
public void setOwner(String owner) {
    this.owner = owner;
}

/**
 * @return The secret
 */
public String getSecret() {
    return secret;
}

/**
 * @param secret The secret
 */
public void setSecret(String secret) {
    this.secret = secret;
}

/**
 * @return The server
 */
public String getServer() {
    return server;
}

/**
 * @param server The server
 */
public void setServer(String server) {
    this.server = server;
}

/**
 * @return The farm
 */
public int getFarm() {
    return farm;
}

/**
 * @param farm The farm
 */
public void setFarm(int farm) {
    this.farm = farm;
}

/**
 * @return The title
 */
public String getTitle() {
    return title;
}

/**
 * @param title The title
 */
public void setTitle(String title) {
    this.title = title;
}

/**
 * @return The ispublic
 */
public int getIspublic() {
    return ispublic;
}

/**
 * @param ispublic The ispublic
 */
public void setIspublic(int ispublic) {
    this.ispublic = ispublic;
}

/**
 * @return The isfriend
 */
public int getIsfriend() {
    return isfriend;
}

/**
 * @param isfriend The isfriend
 */
public void setIsfriend(int isfriend) {
    this.isfriend = isfriend;
}

/**
 * @return The isfamily
 */
public int getIsfamily() {
    return isfamily;
}

/**
 * @param isfamily The isfamily
 */
public void setIsfamily(int isfamily) {
    this.isfamily = isfamily;
}

/**
 * @return The urlM
 */
public String getUrlM() {
    return urlM;
}

/**
 * @param urlM The url_m
 */
public void setUrlM(String urlM) {
    this.urlM = urlM;
}

/**
 * @return The heightM
 */
public String getHeightM() {
    return heightM;
}

/**
 * @param heightM The height_m
 */
public void setHeightM(String heightM) {
    this.heightM = heightM;
}

/**
 * @return The widthM
 */
public String getWidthM() {
    return widthM;
}

/**
 * @param widthM The width_m
 */
public void setWidthM(String widthM) {
    this.widthM = widthM;
}

}

JSON响应:

{
  photos: {
  page: 1,
  pages: 3683,
  perpage: 100,
  total: "368270",
  photo: [
       {
       id: "29264707352",
       owner: "84316756@N02",
       secret: "9ed355a86e",
       server: "8603",
       farm: 9,
       title: "Tercer Patio de los Claustros de la Compañía/ Arequipa",
       ispublic: 1,
       isfriend: 0,
       isfamily: 0,
       url_m:          "https://farm9.staticflickr.com/8603/29264707352_9ed355a86e.jpg",
       height_m: "500",
       width_m: "333"
          },
        {
        id: "29339070436",
        owner: "146617764@N02",
        secret: "b52f1e9914",
        server: "8509",
        farm: 9,
        title: "2016-04-17 09.24.07",
        ispublic: 1,
        isfriend: 0,
        isfamily: 0,
        url_m: "https://farm9.staticflickr.com/8509/29339070436_b52f1e9914.jpg",
        height_m: "281",
        width_m: "500"
       },

LOGCAT

09-03 15:11:33.037 1846-1846/com.troychuinard.flickr_test E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.troychuinard.flickr_test, PID: 1846
                                                                            java.lang.NullPointerException: println needs a message
                                                                                at android.util.Log.println_native(Native Method)
                                                                                at android.util.Log.v(Log.java:118)
                                                                                at com.troychuinard.flickr_test.MainActivity$1$1.onResponse(MainActivity.java:72)
                                                                                at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:135)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
09-03 15:14:21.858 1846-1846/com.troychuinard.flickr_test I/Process: Sending signal. PID: 1846 SIG: 9

72行

         Log.v("RESPONSE_BODY", response.body().getTotal());

问题答案:

您的POJO课程不适合服务器 响应, 因此您的身体为 Null 。您的模型应如下所示:

public class Model {

    // I guess it should be List too -> List<Photos>
    // your response is not complete to see 
    Photos photos;


    public class Photos{
        int page;
        int pages;
        int perpage;
        int total;
        List<Photo> photo;


        //this photo class is inner class of Photos
        public class Photo{
            String id;
            String owner;
            String secret;
            //rest of things

        }
    }
}


 类似资料:
  • 我希望有人能帮我解决这个问题。 我想用WebClient创建一个Rest客户端,从API中检索响应。所以我创建了我的Spring项目,添加了webflux、lombok和h2。我还创建了一个DTO类“CashAccount”和以下方法: 当我使用“.bodyToMono(String.class)”时,所有的功能都很好,我收到了结果: 相反,当我使用“.bodyToMono(cashcount.c

  • 我试图将一个JSON发送给一个REST控制器,它是用Spring编写的,不管我做了什么尝试,Jackson抛出了一个异常: 这是JSON: 有什么建议吗?

  • GSON不是在一开始就解析我传递的整个JSON字符串吗?因此,最终,我希望新的数据源是对象。那可行吗?

  • 下面是我在点击特定Web服务时收到的JSON响应: 我不知道哪个索引有我的期望值(我需要在确定哪个具有)后验证多个值),这是动态的。不希望使用硬编码值。并匹配,因为这将在下次更改。 对此我有两个问题: 如何将响应传递给java代码并获取具有的数组索引,以便使用此索引进行验证? 下面的代码不起作用。

  • 我有一个json转储为 我编写了一个小代码来从json获取一些详细信息 这给我造成了错误: 回溯(最近一次调用):文件“python_opennms.py”,第24行,in x=get_nodes_opennms()文件“python_opennms.py”,第15行,in get_nodes_opennms for i in JSON.load(open('out.txt'))[“alarm”]