使用Gson返回空值的嵌套Json

时间:2018-04-17 01:00:52

标签: java json gson

我正在研究的项目是尝试与FDA API进行通信并检索某些信息。但是,使用我的嵌套JSON,我尝试检索的对象返回null值。有谁知道我做错了什么?谢谢,如果我遗漏任何信息,请告诉我。

网址我正在尝试访问以下信息:
https://api.fda.gov/drug/label.json

JSON Tester类

package com.example.user.searchbarapp;

import java.net.*;
import java.io.*;
import com.google.gson.*;

public class JSONTester {    
    public static void main(String[] args) throws Exception {
        Gson g = new Gson();
        URL FDAServer = new URL("https://api.fda.gov/drug/label.json?");
        HttpURLConnection conn = (HttpURLConnection)
        FDAServer.openConnection();
        conn.setRequestProperty("User-Agent", "Mozilla/5.0");
        InputStreamReader inputStream = new InputStreamReader(conn.getInputStream(), "UTF-8");
        Medication advil = g.fromJson(inputStream, Medication.class);
        System.out.print(advil);
    }
}

药物类

package com.example.user.searchbarapp;

import java.net.URL;
import java.util.Arrays;

public class Medication {

    private NestedMedication[] NestedMedication;

    public Medication(com.example.user.searchbarapp.NestedMedication[] nestedMedication) {
        NestedMedication = nestedMedication;
    }

    public com.example.user.searchbarapp.NestedMedication[] getNestedMedication() {
        return NestedMedication;
    }

    public void setNestedMedication(com.example.user.searchbarapp.NestedMedication[] nestedMedication) {
        NestedMedication = nestedMedication;
    }

    @Override
    public String toString() {
        return "Medication{" +
                "NestedMedication=" + Arrays.toString(NestedMedication) +
                '}';
    }
}

NestedMedication Class

package com.example.user.searchbarapp;

public class NestedMedication {

    String route;

    @Override
    public String toString() {
        return "NestedMedication{" +
                "route='" + route + '\'' +
                '}';
    }
}

输出

Medication{NestedMedication=null}
Process finished with exit code 0

0 个答案:

没有答案