Conversión entre JSON, cadena JSON, lista y mapa
1. Convierta la cadena JSON a Lista <clase de entidad>
Lista<clase de entidad> pojoList = JSONArray.parseArray(jsonStr,entity class.class);
1. Parámetros pasados por el front-end:
String jsonStr1 = "[{"ifCheck":2,"questOptionContentByOne":"答案1","optSort":"A"},{"ifCheck":2,"questOptionContentByOne":"答案1","optSort":"B"}]";
2. Conversión de fondo:
String jsonStr1 = "[{\"ifCheck\":2,\"questOptionContentByOne\":\"答案1\",\"optSort\":\"A\"},{\"ifCheck\":2,\"questOptionContentByOne\":\"答案5\",\"optSort\":\"B\"}]";
List<QuestionOptionExam> questionOptionList = JSONArray.parseArray(jsonStr1 ,QuestionOptionExam.class);
2. Convierta la cadena Json a Lista<Mapa<Cadena,Objeto>>
1. Parámetros de interfaz:
String jsonStr = [{
"id":"1","sort":"1"},{
"id":"2","sort":"2"}]
2. Entre bastidores
String jsonStr = "[{\"id\":\"1\",\"sort\":\"1\"},{\"id\":\"2\",\"sort\":\"2\"}]";
List<Map<String,Object>> strList = (List<Map<String, Object>>) JSONArray.parse(jsonStr);
Convertir cadena JSON a mapa
1 、 tallo
3. Asignar a clase de entidad
1. Código de fondo:
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 );
}
3. Resuma el método principal de prueba.
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);
}
4. Clase de entidad:
/**
*考试题目选项表实体类
*/
@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;
}