Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Jackson API解析自定义类列表?_Java_Jackson - Fatal编程技术网

Java 如何使用Jackson API解析自定义类列表?

Java 如何使用Jackson API解析自定义类列表?,java,jackson,Java,Jackson,我正在尝试使用jacksonapi解析一个JSON,如下所示 { "name": "John", "id": 1, "details": [ { "item": 2, "count": 10 }, { "item": 3, "count": 5 }, ] } 目标类定义为: public clas

我正在尝试使用jacksonapi解析一个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
}
公共类提案{
私有字符串名称;
私有整数id;
私有列表详细信息=新建ArrayList();
//二传手和接球手
}
公共类详细信息{
私有整数项;
私有整数计数;
//二传手和接球手
}
我尝试使用本机数组,但没有成功。我应该使用或创建哪些注释和类来实现转换


更新-实际问题

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


在修复了我们的隐藏解析器之后,jackson按照预期工作,并由大家描述。非常感谢你回答我的问题。这让我找到了正确的方向。

有一个非常有用的网站,允许您根据示例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);
}

}
package.com.example;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入com.fasterxml.jackson.annotation.JsonAnyGetter;
导入com.fasterxml.jackson.annotation.JsonAnySetter;
导入com.fasterxml.jackson.annotation.JsonIgnore;
导入com.fasterxml.jackson.annotation.JsonInclude;
导入com.fasterxml.jackson.annotation.JsonProperty;
导入com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
“姓名”,
“身份证”,
“详情”
})
公开课建议{
@JsonProperty(“名称”)
私有字符串名称;
@JsonProperty(“id”)
私有整数id;
@JsonProperty(“详细信息”)
私有列表详细信息=null;
@杰索尼奥雷
私有映射additionalProperties=new HashMap();
@JsonProperty(“名称”)
公共字符串getName(){
返回名称;
}
@JsonProperty(“名称”)
公共void集合名(字符串名){
this.name=名称;
}
@JsonProperty(“id”)
公共整数getId(){
返回id;
}
@JsonProperty(“id”)
公共无效集合id(整数id){
this.id=id;
}
@JsonProperty(“详细信息”)
公共列表getDetails(){
退货详情;
}
@JsonProperty(“详细信息”)
公共无效设置详细信息(列表详细信息){
this.details=详细信息;
}
@JsonAnyGetter
公共映射getAdditionalProperties(){
返回此。附加属性;
}
@JSONANYSETER
public void setAdditionalProperty(字符串名称、对象值){
this.additionalProperties.put(名称、值);
}
}
使用此代码

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;
    }
}
package-com;
导入com.fasterxml.jackson.core.JsonParseException;
导入com.fasterxml.jackson.databind.JsonMappingException;
导入com.fasterxml.jackson.databind.ObjectMapper;
导入com.itextpdf.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
公开课演示{
公共静态void main(字符串[]args){
字符串jsonStr=“{\n”+
“\”姓名\“:\”约翰\“,\n”+
“\“id\”:1\n”+
“\“详细信息\”:[\n”+
“{\n”+
“项目”:2\n+
“\“计数\”:10\n”+
},\n+
“{\n”+
“项目”:3\n+
“\“计数\”:5\n”+
“}\n”+
“]\n”+
"}";
ObjectMapper mapper=新的ObjectMapper();
试一试{
建议书=mapper.readValue(jsonStr,建议书.class);
系统输出打印(建议书);
}捕获(异常ioe){
ioe.printStackTrace();
}
}
}
班级提案{
私有字符串名称;
私有整数id;
私人名单详情;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共列表getDetails(){
退货详情;
}
公共无效设置详细信息(列表详细信息){
this.details=详细信息;
}
//二传手和接球手
}
课堂细节{
私有整数项;
私有整数计数;
公共整数getItem(){
退货项目;
}
公共void集合项(整数项){
this.item=项目;
}
公共整数getCount(){
返回计数;
}
公共无效集合计数(整数计数){
this.count=计数;
}
}

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

你“尝试”的确切程度如何?除了json中的尾随逗号外,看起来没有wrnog。请阅读“如何创建一个”。然后使用链接改进您的问题(不要通过评论添加更多信息)。否则,我们无法回答您的问题并帮助您。观察得很好,但这是一种猜测,如果这真的是他的问题。因为他没有提供任何代码或错误信息。