JSON、JSON字符串和List、Map之间的转换

JSON、JSON字符串和List、Map之间的转换

一、JSON字符串转List<实体类>

List<实体类> pojoList = JSONArray.parseArray(jsonStr,实体类.class);

1、前端传的参数:
String jsonStr1 = "[{"ifCheck":2,"questOptionContentByOne":"答案1","optSort":"A"},{"ifCheck":2,"questOptionContentByOne":"答案1","optSort":"B"}]";
2、后台转换:
String jsonStr1 = "[{\"ifCheck\":2,\"questOptionContentByOne\":\"答案1\",\"optSort\":\"A\"},{\"ifCheck\":2,\"questOptionContentByOne\":\"答案5\",\"optSort\":\"B\"}]";
 
List<QuestionOptionExam> questionOptionList = JSONArray.parseArray(jsonStr1 ,QuestionOptionExam.class);

二、Json字符串转List<Map<String,Object>>

1、前端参数:
String jsonStr = [{
    
    "id":"1","sort":"1"},{
    
    "id":"2","sort":"2"}]
2、后台
String jsonStr = "[{\"id\":\"1\",\"sort\":\"1\"},{\"id\":\"2\",\"sort\":\"2\"}]";

List<Map<String,Object>> strList = (List<Map<String, Object>>) JSONArray.parse(jsonStr);

JSON字符串转Map

1、qia

三、Map转实体类

1、后台代码:
public static void main(String[] args){
    
    
	Map<String,Object> map = new HashMap<>();
	map.put("questOptionId","1");
	map.put("optSort","2");
	map.put("questOptionContentByOne","3");
	map.put("ifCheck","4");
	QuestionOptionExam questionOptionExam = JSON.parseObject(JSON.toJSONString(map),QuestionOptionExam .class);
	System.out.println(questionOptionExam );
}

三、总结测试main方法

public static void main(String[] args){
    
    
        Map<String,Object> map = new HashMap<>();
        map.put("questOptionId","1");
        map.put("optSort","2");
        map.put("questOptionContentByOne","3");
        map.put("ifCheck","4");
        QuestionOptionExam questionOptionExam = JSON.parseObject(JSON.toJSONString(map),QuestionOptionExam .class);
        System.out.println(questionOptionExam );
        String jsonStr = "[{\"id\":\"1\",\"sort\":\"1\"},{\"id\":\"2\",\"sort\":\"2\",\"questOptionId\":\"1\"}]";
        List<Map<String,Object>> strList = (List<Map<String, Object>>) JSONArray.parse(jsonStr);
        System.out.println(strList);
        String jsonStr1 = "[{\"ifCheck\":2,\"questOptionContentByOne\":\"答案1\",\"optSort\":\"A\"},{\"ifCheck\":2,\"questOptionContentByOne\":\"答案5\",\"optSort\":\"B\"}]";
        List<QuestionOptionExam> questionOptionList = JSONArray.parseArray(jsonStr1 ,QuestionOptionExam.class);
        System.out.println(questionOptionList);
    }

四、实体类:

/**
 *考试题目选项表实体类
 */
@Data
//@EqualsAndHashCode(callSuper = true)
//@ApiModel(value = "QuestionOprionExam对象", description ="考试题目选项表实体类")
public class QuestionOptionExam  {
    
    
    private static final long serialVersionUID = 1L;
    /**
     *题目ID
     */
//    @ApiModelProperty(value = "题目选项ID")
    private Long questOptionId;
    /**
     *选项序号
     */
//    @ApiModelProperty(value = "选项序号")
    private String optSort;
    /**
     *选项内容
     */
//    @ApiModelProperty(value = "选项内容")
    private String questOptionContentByOne;
    /**
     *是否选中(1是2否)
     */
//    @ApiModelProperty(value = "是否选中(1是2否)")
    private Integer ifCheck;
    /**
     *备注
     */
//    @ApiModelProperty(value = "备注")
    private String remarks;
}

猜你喜欢

转载自blog.csdn.net/qq_40660283/article/details/116718753