Java 使用Gson返回空值的嵌套Json
我正在从事的项目是尝试与FDA API进行通信并检索某些信息。但是,对于嵌套的JSON,我试图检索的对象返回一个Java 使用Gson返回空值的嵌套Json,java,json,gson,Java,Json,Gson,我正在从事的项目是尝试与FDA API进行通信并检索某些信息。但是,对于嵌套的JSON,我试图检索的对象返回一个null值。有人知道我做错了什么吗?谢谢,如果我遗漏了任何信息,请告诉我 我试图从中访问信息的URL: ? JSON测试仪类 package com.example.user.searchbarapp; import java.net.*; import java.io.*; import com.google.gson.*; public class JSONTester {
null
值。有人知道我做错了什么吗?谢谢,如果我遗漏了任何信息,请告诉我
我试图从中访问信息的URL:? JSON测试仪类
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) +
'}';
}
}
package com.example.user.searchbarapp;
public class NestedMedication {
String route;
@Override
public String toString() {
return "NestedMedication{" +
"route='" + route + '\'' +
'}';
}
}
药物类
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) +
'}';
}
}
package com.example.user.searchbarapp;
public class NestedMedication {
String route;
@Override
public String toString() {
return "NestedMedication{" +
"route='" + route + '\'' +
'}';
}
}
嵌套医疗类
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) +
'}';
}
}
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
您链接的json没有
nestedmediation
节点。你的意思是结果
?我需要用我试图访问的json节点命名我的类吗?我想如果我在NestedMedical中定义了route,它就会访问它