我爱编程

com.alibaba.fastjson

2018-05-28  本文已影响0人  青丝如梦

JSON

String jsonString = JSON.toJSONString(userDo);
String jsonArrayString = JSON.toJSONString(userDoList);
HashMap hashMap = JSON.parseObject(jsonObject, HashMap.class);
byte[] bytes = ...;
JSON.parseObject(bytes, UserBo.class)
JSON.toJSONString(Object object);
//当 JSON 对象中有 null 值时,直接调用 .toJSONString() 方法会自动过滤 null 值,需要进行如下操作:
JSONObject jsonContent = ...
String jsonString = JSON.toJSONString(jsonContent, SerializerFeature.WriteMapNullValue) //此处获取到的 json 是包含 null 值的

JSONObject

jsonString转JSONObject:

JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(userOperationLogDo));

JSONObject转Java Class:

Map<String, Object> map = JSONObject.toJavaObject(jsonObject, Map.class);

jsonString转List<>

// 不带泛型的Map
List<Map> lsitMaps = JSONObject.parseArray(jsonString, Map.class);

JSONArray

JSON转JSONArray:

JSONArray jsonArray = JSONArray.fromObject("[{},{}]");

JSONArray 转 List<>:

// 带泛型的Map
List<Map<String, Object>> mapList = JSONArray.parseObject(jsonString, new TypeReference<List<Map<String, Object>>>() {});
// 转JavaBean
List<User> userList = JSONArray.parseArray(jsonString, User.class);

List转JSONArray:

List<UserBo> list = new ArrayList();
JSONArray.parseArray(JSON.toJSONString(list));

Json 字符串转 List

List<User> userList = JSON.parseArray(userStr, User.class);
上一篇 下一篇

猜你喜欢

热点阅读