【问题标题】:How to parse a custom class list using Jackson API?如何使用 Jackson API 解析自定义类列表?
【发布时间】:2019-06-27 11:56:15
【问题描述】:

我正在尝试使用 Jackson API 解析如下所示的 JSON。

{
    "name": "John", 
    "id": 1, 
    "details": [
        {
            "item": 2, 
            "count": 10
        },
        {
            "item": 3, 
            "count": 5
        },
    ]
}

目标类定义为:

public class Proposal {

    private String name;

    private Integer id;

    private List<Detail> details = new ArrayList<>();

    // Setters and getters
}

public class Detail {

    private Integer item;

    private Integer count;

    // Setters and Getters
}

我尝试使用原生数组,但没有成功。我应该使用或创建哪些注释和类来使转换正常工作?


更新 - 实际问题

不幸的是,我用来发送 json 消息的过程是将消息内容解析为用 AspectJ 编写的隐藏方法。帖子中的格式错误的消息只是一个错字,系统中不存在。

修复我们的隐藏解析器后,jackson 将按预期工作并在此处描述。非常感谢您回答我的问题。这让我朝着正确的方向前进。

【问题讨论】:

  • 你在“尝试”到底如何?除了 json 中的尾随逗号外,看起来并没有错。
  • 请阅读“如何创建minimal reproducible example”。然后使用edit 链接改进您的问题(不要通过 cmets 添加更多信息)。否则我们无法回答您的问题并为您提供帮助。

标签: java jackson


【解决方案1】:

使用此代码

package com;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itextpdf.io.IOException;

import java.util.ArrayList;
import java.util.List;

public class Demo {
    public static void main(String[] args) {
        String jsonStr = "{\n" +
                "    \"name\": \"John\", \n" +
                "    \"id\": 1, \n" +
                "    \"details\": [\n" +
                "        {\n" +
                "            \"item\": 2, \n" +
                "            \"count\": 10\n" +
                "        },\n" +
                "        {\n" +
                "            \"item\": 3, \n" +
                "            \"count\": 5\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        ObjectMapper mapper = new ObjectMapper();
        try{
            Proposal proposal =  mapper.readValue(jsonStr,Proposal.class);
            System.out.println(proposal);
        }catch(Exception ioe){
            ioe.printStackTrace();
        }

    }
}


class Proposal {

    private String name;

    private Integer id;

    private List<Detail> details;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public List<Detail> getDetails() {
        return details;
    }

    public void setDetails(List<Detail> details) {
        this.details = details;
    }

    // Setters and getters
}

class Detail {

    private Integer item;

    private Integer count;

    public Integer getItem() {
        return item;
    }

    public void setItem(Integer item) {
        this.item = item;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }
}

在你的 json 字符串中,一个逗号是多余的

【讨论】:

  • 很好的观察,但如果这真的是他的问题,那就有点猜测了。因为他没有提供任何代码或错误信息。
【解决方案2】:

有一个非常有用的网站jsonschema2pojo 允许您基于示例 JSON 生成 java 代码。鉴于它生成的示例:

package com.example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"name",
"id",
"details"
})
public class Proposal {

@JsonProperty("name")
private String name;
@JsonProperty("id")
private Integer id;
@JsonProperty("details")
private List<Detail> details = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("name")
public String getName() {
return name;
}

@JsonProperty("name")
public void setName(String name) {
this.name = name;
}

@JsonProperty("id")
public Integer getId() {
return id;
}

@JsonProperty("id")
public void setId(Integer id) {
this.id = id;
}

@JsonProperty("details")
public List<Detail> getDetails() {
return details;
}

@JsonProperty("details")
public void setDetails(List<Detail> details) {
this.details = details;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    相关资源
    最近更新 更多