我正在调用第三方 REST 服务来获取求职者详细信息,它以 JSON 格式发回响应。 JSON 数据看起来像
[
{
"Id": 1
"County": "sample string 16",
"Country": "sample string 17",
"AtAddressSinceMonth": 1,
"AtAddressSinceYear": 1,
"AtAddressUntilMonth": 1,
"AtAddressUntilYear": 1,
"DateOfBirth": "2013-12-11T14:25:27.3753327-05:00",
"Documents": [
{
"Type": "sample string 1",
"Description": "sample string 2",
"File": "sample string 3"
},
{
"Type": "sample string 1",
"Description": "sample string 2",
"File": "sample string 3"
},
],
"Events": [
{
"StartDateTime": "2013-12-11T14:25:27.3753327-05:00",
"EndDateTime": "2013-12-11T14:25:27.3753327-05:00",
"Description": "sample string 3",
"Attendees": [
{
"EmailAddress": "sample string 1",
"FirstName": "sample string 2",
"LastName": "sample string 3"
},
{
"EmailAddress": "sample string 1",
"FirstName": "sample string 2",
"LastName": "sample string 3"
}
]
},
{
"StartDateTime": "2013-12-11T14:25:27.3753327-05:00",
"EndDateTime": "2013-12-11T14:25:27.3753327-05:00",
"Description": "sample string 3",
"Attendees": [
{
"EmailAddress": "sample string 1",
"FirstName": "sample string 2",
"LastName": "sample string 3"
},
....
....
{
"Id": 1,
"County": "sample string 16",
"Country": "sample string 17",
"AtAddressSinceMonth": 1,
"AtAddressSinceYear": 1,
"AtAddressUntilMonth": 1,
"AtAddressUntilYear": 1,
...
...
}
]
}
]
就这样继续下去..我定义了一个静态类JobSeeker,其中包含所有变量和所有嵌套变量的静态内部类..以及各自的set和get方法..
此外,还使用变量 protected List jobSeeker 创建了另一个类 ArrayOfJobSeeker;我的类定义如下
public class ArrayOfJobSeeker {
protected List<ArrayOfJobSeeker.JobSeeker> jobSeeker;
public List<ArrayOfJobSeeker.JobSeeker> getJobSeeker() {
if (jobSeeker == null) {
jobSeeker = new ArrayList<ArrayOfJobSeeker.JobSeeker>();
}
return this.jobSeeker;
}
public static class JobSeeker {
protected String address;
protected String atAddressSinceMonth;
protected String atAddressSinceYear;
protected String atAddressUntilMonth;
protected String atAddressUntilYear;
protected String city;
protected String country;
protected String county;
protected XMLGregorianCalendar dateOfBirth;
protected ArrayOfJobSeeker.JobSeeker.Documents documents;
protected String emailAddress;
protected ArrayOfJobSeeker.JobSeeker.Events events;
protected String extension;
protected String firstName;
protected String genderCode;
protected ArrayOfJobSeeker.JobSeeker.HistoryActivities historyActivities;
protected String id;
protected String lastName;
protected String maidenName;
protected String middleName;
和嵌套内部类
public static class Documents {
protected List<ArrayOfJobSeeker.JobSeeker.Documents.Document> document;
public List<ArrayOfJobSeeker.JobSeeker.Documents.Document> getDocument() {
if (document == null) {
document = new ArrayList<ArrayOfJobSeeker.JobSeeker.Documents.Document>();
}
return this.document;
}
public static class Document {
protected String description;
protected String file;
protected String type;
} //end Document
} // end Documents
}//求职结束
我不太确定如何使用 GSON 解析这个嵌套数组,因为此代码会抛出错误
File file = new File("c:/temp/testJSON.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
Gson gson = new Gson();
gson.fromJson(br, ArrayOfJobSeeker.class)
请帮忙。
谢谢
最佳答案
引用您在问题中的评论,您的代码是否真的打印了整个 json 字符串?我认为你只会得到第一个元素。
如果可能的话,我建议你取出内部类,然后做类似的事情
JsonArray jarray = new JsonParser().parse(json).getAsJsonArray();
for (JsonElement e : jarray) {
JsonObject jobSeeker = e.getAsJsonObject();
JsonArray events = e.get("Events").getAsJsonArray();
JsonArray documents = e.get("Documents").getAsJsonArray();
// iterate the array, deserialize the objects and do your work
jobSeeker.remove("Events");
jobSeeker.remove("Documents");
// deal with the job seeker;
}
这似乎不是一个好方法,但我认为它应该有效。
关于Java GSON解析——数组和嵌套类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20530936/