gpt4 book ai didi

java - 使用 Jackson 解析 JSON 节点内部的 JSON 节点

转载 作者:太空宇宙 更新时间:2023-11-04 06:59:57 24 4
gpt4 key购买 nike

所以我是 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;
}

}

最佳答案

代码:

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

关于java - 使用 Jackson 解析 JSON 节点内部的 JSON 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22235569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com