所以我是Jackson库的新手,我试图在JSON文件中解析一个节点,里面有其他节点,所以例如,它看起来像这样:
{ "node1": "value1",
"node2": "value2",
"node3": {
"innerNode1": "value1",
"innerNode2": "value2"
}
}
我希望能够做的是在类中定义整个节点,但是让node3成为它自己独立的类,它就像使用Jackson解析的常规Java对象一样,所以它具有所有相同的setter和getter方法。所以我希望能够做一些事情,比如致电masterNode.getNode1
并输出value1
,但也可以调用node3.getInnerNode1
并输出value1
我不确定如何做到这样的事情,任何帮助都会受到赞赏!
编辑:这是我正在使用的代码:
protected void onPostExecute(ContactInfo[] result) {
int length = result.length;
ArrayList<ContactInfo> contacts = new ArrayList<ContactInfo>();
ArrayList<String> names = new ArrayList<String>();
ArrayList<PhoneNumber> numberMap = new ArrayList<PhoneNumber>();
ArrayList<String> workNumber = new ArrayList<String>();
for (int i = 0; i < length; i++) {
contacts.add(result[i]);
}
for (int i = 0; i < length; i++) {
names.add(contacts.get(i).getName());
}
for (int i = 0; i < length; i++) {
numberMap.add(contacts.get(i).getPhoneNumber());
}
for(PhoneNumber num: numberMap) {
workNumber.add(num.getWork());
}
adapter = new ArrayAdapter<String>(local, R.layout.list_item, R.id.contactName, workNumber);
setListAdapter(adapter);
}
就一切顺利而言,在我试图从PhoneNumber类中获取实际数字之前没有任何细分,这就是它给我一个NullPointerError的地方。我有类设置,但它们很长,因为有很多字段,但我已经测试了这个代码并且它正确地抓住了东西,所有这些,故障是在尝试从PhoneNumber类中抓取东西时(即“工作”号码。)
编辑:ContactInfo类:
package com.example.codingchallenge;
import java.util.Map;
public class ContactInfo {
private String name, company, detailsURL, smallImageURL;
private int employeeId, birthdate;
private PhoneNumber phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getDetailsURL() {
return detailsURL;
}
public void setDetailsURL(String detailsURL) {
this.detailsURL = detailsURL;
}
public String getSmallImageURL() {
return smallImageURL;
}
public void setSmallImageURL(String smallImageURL) {
this.smallImageURL = smallImageURL;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public int getBirthdate() {
return birthdate;
}
public void setBirthdate(int birthdate) {
this.birthdate = birthdate;
}
public PhoneNumber getPhoneNumber() {
return phone;
}
public void setPhoneNumber(PhoneNumber phone) {
this.phone = phone;
}
}
答案 0 :(得分:1)
代码:
public static class ContactInfo {
private String name;
private PhoneNumber phoneNumber;
public String getName() {
return name;
}
public PhoneNumber getPhoneNumber() {
return phoneNumber;
}
public void setName(String name) {
this.name = name;
}
public void setPhoneNumber(PhoneNumber phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
public static class PhoneNumber {
private String work;
public String getWork() {
return work;
}
public void setWork(String work) {
this.work = work;
}
}
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
// POJO
ContactInfo info1 = new ContactInfo();
info1.setName("Neel1");
PhoneNumber num = new PhoneNumber();
num.setWork("123 456 7890");
info1.setPhoneNumber(num);
String json = new ObjectMapper().defaultPrettyPrintingWriter().writeValueAsString(info1);
System.out.println(json);
// From JSON
String info2Json = "{ \"name\" : \"Neel2\", \"phoneNumber\" : { \"work\" : \"098 765 4321\" } }";
ContactInfo info2 = new ObjectMapper().readValue(info2Json, ContactInfo.class);
doSomething(new ContactInfo[] { info1, info2 });
}
private static void doSomething(ContactInfo[] result) {
for(ContactInfo each : result) {
String name = each.getName();
String workPhone = each.getPhoneNumber().getWork();
doSomething(name, workPhone);
}
}
private static void doSomething(String name, String workPhone) {
System.out.println("name: " + name + ", work: " + workPhone);
}
输出:
{
"name" : "Neel1",
"phoneNumber" : {
"work" : "123 456 7890"
}
}
name: Neel1, work: 123 456 7890
name: Neel2, work: 098 765 4321