Jackson
概述:Jackson 是一个 Java 的用来处理 JSON 格式数据的类库
三大功能:
- jackson-core,核心包,提供基于"流模式"解析的相关 API,它包括 JsonPaser 和 JsonGenerator。 Jackson 内部实现正是通过高性能的流模式 API 的 JsonGenerator 和 JsonParser 来生成和解析 json。
- jackson-annotations,注解包,提供标准注解功能;
- jackson-databind ,数据绑定包, 提供基于"对象绑定" 解析的相关 API ( ObjectMapper ) 和"树模型" 解析的相关 API (JsonNode);基于"对象绑定" 解析的 API 和"树模型"解析的 API 依赖基于"流模式"解析的 API。
ObjectMapper
Jackson 最常用的 API 就是基于"对象绑定" 的 ObjectMapper。下面是一个 ObjectMapper 的使用的简单示例。
- ObjectMapper 通过 writeValue 系列方法 将 java 对 象序列化 为 json,并 将 json 存 储成不同的格式,String(writeValueAsString),Byte Array(writeValueAsString),Writer, File,OutStream 和 DataOutput。
- ObjectMapper 通过 readValue 系列方法从不同的数据源像 String , Byte Array, Reader,File,URL, InputStream 将 json 反序列化为 java 对象。
简单对象的转化和集合对象的转化:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jt.pojo.ItemDesc;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class TestObjectMapper {
@Test//测试简单对象的转化
public void test01() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
ItemDesc itemDesc = new ItemDesc();
itemDesc.setItemId(100L).setItemDesc("商品详情信息")
.setCreated(new Date()).setUpdated(new Date());
//对象转化为json
String json = objectMapper.writeValueAsString(itemDesc);
System.out.println(json);
//json转化为对象
ItemDesc itemDesc2 = objectMapper.readValue(json, ItemDesc.class);
System.out.println(itemDesc2.getItemDesc());
}
@Test//测试集合对象的转化
public void test02() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
ItemDesc itemDesc = new ItemDesc();
itemDesc.setItemId(100L).setItemDesc("商品详情信息1")
.setCreated(new Date()).setUpdated(new Date());
ItemDesc itemDesc2 = new ItemDesc();
itemDesc2.setItemId(100L).setItemDesc("商品详情信息2")
.setCreated(new Date()).setUpdated(new Date());
List<ItemDesc> lists = new ArrayList<>();
lists.add(itemDesc);
lists.add(itemDesc2);
//[{key:value},{}]
String json = objectMapper.writeValueAsString(lists);
System.out.println(json);
//将json串转化为对象
List<ItemDesc> list2 = objectMapper.readValue(json, lists.getClass());
System.out.println(list2);
}
}
工具API:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.util.StringUtils;
public class ObjectMapperUtil {
/**
* 1.将用户传递的数据转化为json串
* 2.将用户传递的json串转化为对象
*/
private static final ObjectMapper MAPPER = new ObjectMapper();
//1.将用户传递的数据转化为json串
public static String toJSON(Object object){
if(object == null) {
throw new RuntimeException("传递的数据为null.请检查");
}
try {
String json = MAPPER.writeValueAsString(object);
return json;
} catch (JsonProcessingException e) {
//将检查异常,转化为运行时异常
e.printStackTrace();
throw new RuntimeException(e);
}
}
//需求: 要求用户传递什么样的类型,我返回什么样的对象 泛型的知识
public static <T> T toObj(String json,Class<T> target){
if(StringUtils.isEmpty(json) || target ==null){
throw new RuntimeException("参数不能为null");
}
try {
return MAPPER.readValue(json, target);
} catch (JsonProcessingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
FastJson
JSON.parseObject的几种用法
FastJson对于json格式的字符串Json对象以及JavaBeen之间的相互转化
一:Json格式字符串与对象之间的转化:
1:result的格式:
{
"success":"true",
"returnAddress":"123"
}
JSONObject jsonObject=JSON.parseObject(result); //转换成object
jsonObject.getString("returnAddress") //获取object中returnAddress字段;
2:result格式:
{
"success":"true",
"data":{
"shop_uid":"123"
}
}
JSONObject shop_user =JSON.parseObject(result);
JSON.parseObject(shop_user.getString("data")).getString("shop_uid")
3:result格式:
{
"success":"true",
"data":[{
"shop_uid":"123"
},
{
"shop_name":"张三"
}]
}
JSONArray detail = JSON.parseArray(result);
for (int i=0; i<detail.size();i++){
if(detail.get(i)!=null||!detail.get(i).equals("")){
JSONArray detailChild =detail.getJSONArray(i);
if(detailChild.getInteger(1)>Integer.valueOf(ship.get("shiptime").toString())){
ship.put("shiptime",detailChild.getInteger(1));
ship.put("desc",detailChild.getString(0));
}
}
}
二:JSON转javaBean
JSONobject=>javaBean
JSONObject contentChild = contentsArray.getJSONObject(i);
QCCustomerScore.CustomerCore customerCore = JSON
.toJavaObject(contentChild, QCCustomerScore.CustomerCore.class);