Reputation: 2826
So I'm new to the Jackson library and am trying to parse a node in a JSON file which has other nodes inside of it, so for example, it would look like this:
{ "node1": "value1",
"node2": "value2",
"node3": {
"innerNode1": "value1",
"innerNode2": "value2"
}
}
What I want to be able to do is have the entire node defined inside of a class, but then have node3 be it's own separate class which acts just like a regular Java Object parsed with Jackson, so it has all the same setter and getter methods. So I would like to be able to do something like call masterNode.getNode1
and have it output value1
, but also be able to call node3.getInnerNode1
and have it output value1
I'm not sure how something like this would be done, any help would be appreciated!
Edit: Here is the code I'm using:
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);
}
As far as everything goes, nothing breaks down until I get to trying to get the actual number out of the PhoneNumber class, that's where it gives me a NullPointerError. I have the classes setup, however they are quite long as there are a lot of fields, but I have tested this code and it is grabbing things correctly, and all of that, the breakdown is when trying to grab things out of the PhoneNumber class (i.e. the "work" number.)
Edit: ContactInfo class:
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;
}
}
Upvotes: 1
Views: 961
Reputation: 2100
Code:
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);
}
Output:
{
"name" : "Neel1",
"phoneNumber" : {
"work" : "123 456 7890"
}
}
name: Neel1, work: 123 456 7890
name: Neel2, work: 098 765 4321
Upvotes: 1