其他分享
首页 > 其他分享> > 将json参数存放到实体类中

将json参数存放到实体类中

作者:互联网

// 转json对象存入实体类

//	方式一
ObjectMapper mapper=new ObjectMapper();
responseOrderInfo = mapper.readValue("JOSN字符串", "实体类".class);

//	方式二
responseOrderInfo = JSON.parseObject("JOSN字符串", "实体类".class);

注:
实体类要加@JsonProperty注解,用来序列化另外一个名称,属性名不一致会导致找不到。比如JSON中的参数user_name,实体类为userName, @JsonProperty(value=“user_name”)。
如有报错:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “XXXX” (class com.XXXX.entity.XXXXX), not marked as ignorable (84 known…
实体类中加@JsonIgnoreProperties(ignoreUnknown = true)注解

package com.chemscene.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseOrderInfo {
    @JsonProperty(value="user_name")
    private String userName;
    @JsonProperty(value="password")
    private String inventorycountry;
    @JsonProperty(value="age")
    private String age;
    @JsonProperty(value="sex")
    private String sex;
    @JsonProperty(value="hobby")
    private String hobby;
    @JsonProperty(value="address")
    private String address;
    @JsonProperty(value="Phone")
    private String Phone;

//此处需加set方法
	

标签:JsonProperty,实体类,String,value,json,存放,private,com
来源: https://blog.csdn.net/weixin_45119459/article/details/120830913