如何将json数组映射到java类

时间:2017-06-22 07:34:21

标签: java spring-boot

我正在尝试将请求正文从邮递员映射到弹簧启动实体,

{
"college":"0",
"collegeName":"",
"email":"a.r@g.com",
"firstName":"Mike",
"gradEndYear":"2017",
"gradStartYear":"2014",
"lastName":"worrell",
"marks":[
    {"sem1":60},
    {"sem2":70},
    {"sem3":80},
    {"sem4":60},
    {"sem5":60},
    {"sem6":60},
    {"sem7":70},
    {"sem8":60}
    ]
}

问题是我试图将标记数组映射到地图但是我收到以下错误

  

message":" JSON解析错误:无法反序列化实例   java.util.LinkedHashMap超出START_ARRAY令牌;嵌套异常是   com.fasterxml.jackson.databind.JsonMappingException:不能   从START_ARRAY反序列化java.util.LinkedHashMap的实例   token \ n at [来源:java.io.PushbackInputStream@2b05836e;行:9,   专栏:9](通过参考链:   icms.external.springboot.candidateRegistration.model.CandidateRegistrationForm [\"马克\"])

Java模型

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class CandidateRegistrationForm
{
    private String firstName;
    private String middleName;
    private String lastName;
    private String email;
    private String college;
    private String collegeName;
    private String gradStartYear;
    private String gradEndYear;

    private Map<String, Integer> marks = new HashMap<>();

//getters and setters
  }

3 个答案:

答案 0 :(得分:0)

要么改变你的json以便它可以映射到java地图数据结构,那么json就像 "marks": { "sem1":60, "sem2":70, }

或使用arraylist而不是地图。

答案 1 :(得分:0)

Map由JSON对象表示,而不是数组。因此你应该改变你的json:

{
"college":"0",
"collegeName":"",
"email":"a.r@g.com",
"firstName":"Mike",
"gradEndYear":"2017",
"gradStartYear":"2014",
"lastName":"worrell",
"marks":[
    {"sem1":60},
    {"sem2":70},
    {"sem3":80},
    {"sem4":60},
    {"sem5":60},
    {"sem6":60},
    {"sem7":70},
    {"sem8":60}
    ]
}

进入

{
"college":"0",
"collegeName":"",
"email":"a.r@g.com",
"firstName":"Mike",
"gradEndYear":"2017",
"gradStartYear":"2014",
"lastName":"worrell",
"marks":{
    {"sem1":60},
    {"sem2":70},
    {"sem3":80},
    {"sem4":60},
    {"sem5":60},
    {"sem6":60},
    {"sem7":70},
    {"sem8":60}
    }
}

这是JSON中Map的表示方式。如果您想保留此JSON,请将您的课程更改为:

private String firstName;
private String middleName;
private String lastName;
private String email;
private String college;
private String collegeName;
private String gradStartYear;
private String gradEndYear;
private List<Map<String, Integer>> marks = new ArrayList<Map<String, Integer>>();

然后你可以轻松地平面映射它:

Map<String, Integer> map = list.stream()
                                .flatMap(m -> m.entrySet().stream())
                                .collect(Collectors.toMap(
                                         Map.Entry::getKey, 
                                         Map.Entry::getValue));

答案 2 :(得分:0)

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class CandidateRegistrationForm
{
    private String firstName;
    private String middleName;
    private String lastName;
    private String email;
    private String college;
    private String collegeName;
    private String gradStartYear;
    private String gradEndYear;
    private List<LinkedHashMap<String, Integer>> marks = new ArrayList<LinkedHashMap<String, Integer>>();

//getters and setters
  }

标记应该是json对象的列表,我的意思是map<String, Integer>可以被视为{"sem1": 60}list<LinkedHashMap<String, Integer>>将被视为[{"sem1":60}]