简体   繁体   English

使用Jackson解析JSON节点内的JSON节点

[英]Use Jackson to parse JSON nodes inside a JSON node

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: 因此,我是Jackson图书馆的新手,正在尝试解析JSON文件中的一个节点,该文件中包含其他节点,因此,例如,它看起来像这样:

{ "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. 我想要做的是将整个节点定义在一个类中,但是然后将node3作为它自己的单独类,其行为就像用Jackson解析的常规Java对象一样,因此它具有相同的setter和getter方法。 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 因此,我希望能够执行类似调用masterNode.getNode1并使其输出value1 ,但也能够调用node3.getInnerNode1并使其输出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. 就一切而言,直到我尝试从PhoneNumber类中获取实际号码之前,一切都没有破裂,这就是给我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 (ie the "work" number.) 我已经设置了类,但是只要有很多字段,它们就很长了,但是我已经测试了这段代码,它可以正确地抓取东西,而所有这些,击穿是当试图从PhoneNumber类中抓取东西时(即“工作”编号)。

Edit: ContactInfo class: 编辑: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;
}

} }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM