一、转map
使用JSONArray.parseObject方法
引入包:import com.alibaba.fastjson.JSONArray; 及 import java.util.Map;
json格式如下:(DB中的存储形式)
{
"size":"XL",
"color":"臧红",
"material":"纯棉",
"colorCode":"1006",
"styleCode":"553682"
}
代码转换
@Test
public void testTransform() {
String jsonMap = "{\n" +
"\t\"size\": \"XL\",\n" +
"\t\"color\": \"臧红\",\n" +
"\t\"colorCode\": \"1006\",\n" +
"\t\"styleCode\": \"553682\",\n" +
"\t\"material\": \"纯棉\"\n" +
"}";
Map mapTypes = JSONArray.parseObject(jsonMap);
String size = mapTypes.get("size") != null ? mapTypes.get("size").toString() : "";
String color = mapTypes.get("color") != null ? mapTypes.get("color").toString() : "";
String colorCode = mapTypes.get("colorCode") != null ? mapTypes.get("colorCode").toString() : "";
String styleCode = mapTypes.get("styleCode") != null ? mapTypes.get("styleCode").toString() : "";
String material = mapTypes.get("material") != null ? mapTypes.get("material").toString() : "";
System.out.println("-----------【Start】测试打印-------------");
System.out.println("尺寸:" + size + ";颜色:" + color + ";颜色号码:" + colorCode + ";款式编码:" + styleCode + ";材质:" + material);
System.out.println("-----------【END】测试打印-------------");
}
打印:
二、转list
第一步:定义一个list的json
数据库定义如下:ALTER TABLE people ADD `student` json DEFAULT NULL COMMENT '学生信息';
第二步:定义与之对应的对象
package Entity;
import java.util.List;
/**
* @autor Star
* @date 2020-03-03 14:37
*/
public class Student {
private String name;
private String number;
private List<String> hobby;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", number='" + number + '\'' +
", hobby=" + hobby +
'}';
}
}
第三步:自己实现转换的工具
maven引入依赖
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.3</version>
</dependency>
转换工具
package common;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @autor Star
* @date 2020-03-03 14:21
*/
public class JacksonUtils {
private final static ObjectMapper om = new ObjectMapper();
public static <T> T fromJson(String content, TypeReference typeReference) {
try {
return om.readValue(content, typeReference);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
第四步:测试
import Entity.Student;
import com.fasterxml.jackson.core.type.TypeReference;
import common.JacksonUtils;
import org.junit.Test;
import org.springframework.util.CollectionUtils;
import java.util.List;
/**
* @autor Star
* @date 2020-03-03 14:34
*/
public class TestJsonTransform {
@Test
public void TestJsonToList() {
String json="[\n" +
"\t{\n" +
"\t\t\"name\": \"张杰\",\n" +
"\t\t\"number\": \"20201108\",\n" +
"\t\t\"hobby\": [\"打篮球\",\"唱歌\",\"跳舞\"]\n" +
"\t},{\n" +
"\t\t\"name\": \"谢娜\",\n" +
"\t\t\"number\": \"20201109\",\n" +
"\t\t\"hobby\": [\"主持\",\"表演\",\"跳舞\"]\n" +
"\t}\n" +
"]";
List<Student> strList = JacksonUtils.fromJson(json,new TypeReference<List<Student>>(){});
if(CollectionUtils.isEmpty(strList)){
System.out.println("json为空");
}
System.out.println(strList);
}
}
打印:
三、总结
1、如果处理的业务比较复杂,向DB中存储的时候需要json进行存储,那么对数据处理的时候,就存在json转对象,转map和list非常常见,先记录在这。
2、在写代码的时候要引入用到的包以及相应的maven依赖。