文章目录
com.alibaba.fastjson.com
JSONObject
1、json格式的字符串转JSON对象
带转义和引号的字符串转换:
String jsonStr="{\"name\":\"zhangsan\",\"age\":\"123\"}";
JSONObject jsonObject=JSONObject.parseObject(jsonStr);
不带转义和引号的字符串转换:
//不带转义的json格式字符串转换{success=0, msgid=1000001, msg=appkey错误或不存在}
String str="{success=0, msgid=1000001, msg=appkey错误或不存在}";
String input = StringUtils.substringBetween(str, "{", "}"); // substringBetween 截取两个标签之间的文本 input="success=0, msgid=1000001, msg=appkey错误或不存在"
Map<String, String> map = Splitter.on(",").withKeyValueSeparator("=").split(input); // 指定以,为连接的input字符串,withKeyValueSeparator 指定键值对中的分隔符
System.out.println(JSON.toJSONString(map)); // {"success":"0"," msgid":"1000001"," msg":"appkey错误或不存在"}
不带引号的字符串,需要用到guava分割后转:https://blog.csdn.net/chinabestchina/article/details/89290494
2、通过key获取JSON对象的值
JSONObject jsonObject=JSONObject.parseObject(jsonStr);
//获取key为name的值
String name=jsonObject.get("name");
3、JSON对象转某个对象
JSON格式字符串转为实体对象
jsonStr为JSON格式字符串,Employee 为要转为的对象
String jsonStr= "{\"name\":\"张三\",\"age\":18}";
Employee emp=JSONObject.parseObject(jsonStr, Employee.class);
这个方法会出现一个问题,某些值会丢失。
替代方法,先转为Map,然后直接通过get取值
Map<String, Object> map = JSON.parseObject(json, Map.class);
map.get("name");
JSONObject转为实体对象
String jsonString = "{\"name\":\"张三\",\"age\":18}";
//JSON格式字符串转为JSONObject
JSONObject jsonObject = JSONObject.parseObject(jsonString);
//JSONObject转为实体对象
User user = jsonObject.toJavaObject(User.class);
4、对象转JSON
JSONObject json=(JSONObject) JSONObject.toJSON(res);
5、List转JSON
List<User> users= new ArrayList<>();
for (int i=1;i<=100;i++){
User user=new User();
user.setUserName("张"+i);
user.setPassWord("pwd"+i);
users.add(user);
}
String strs= JSON.toJSONString(users);
6、Map和JSON转换
6.1 、Map转JSON
Map<String,String> map = new HashMap<String,String>();
map.put("name","zhangsan");
map.put("age","123");
map.put("sex","1");
String json=JSON.toJSONString(map); //map先转string
JSONObject jsonObject = JSONObject.parseObject(json); //string再转JSONObject
System.out.println(jsonObject); // {"sex":"1","name":"zhangsan","age":"123"}
或者直接
Map<String,String> map = new HashMap<String,String>();
map.put("name","zhangsan");
map.put("age","123");
map.put("sex","1");
JSONObject jsonObject = new JSONObject(map);
System.out.println(jsonObject); // {"sex":"1","name":"zhangsan","age":"123"}
6.2 、JSON转Map
Map map1=JSON.parseObject(JSON.toJSONString(jsonObject)); //json先转string,再转Map
map1.forEach((k,v)->{
System.out.println(k+":"+v);
});
new ObjectMapper()转对象为JSON格式字符串
User user = new User("张三", 20);
String jsonUser = new ObjectMapper().writeValueAsString(user);
System.out.println(jsonUser);
toString后多了斜杠\
使用JSONObject的toString时,多了斜杠\
,直接使用JSONObject.parse
转一下,
使用JSONObject.parse
后
JSONArray
JSONArray转LIST
List<User> users = user.toJavaList(User.class);
List转JSONArray
//list先转JSON,再通过JSONArray的parseArray转成JSONArray
JSONArray.parseArray(JSON.toJSONString(list))
String转JSONArray
//providers="[{"bdid":"4ee415092e1344a18e1864b2fe8ebbe0","winId":"e8b40032886a4e7a8ae33c7f17343e99","remark":""},{"bdid":"840df7c0b9114bc3b17359e69b91a661","winId":"9e600a6dc0604909a943946b3ca7c278","remark":""}]"
JSONArray jsonArray = JSONArray.parseArray(providers);
JSONArray转String
// seal2=[11,12,13]
JSONArray seal2 = (JSONArray) map.get("seal2");
System.out.println(seal2.toString());
//或者直接toString
System.out.println(map.get("seal"+i).toString());
其它
JSON格式的字符串转Map
//String sealStr={"seal0":["1"],"seal1":["4","5"],"seal2":["11","12","13"]}
Map map= (Map) JSON.parse(sealStr);
map.forEach((k,v)->{
System.out.println(k+"--------------------"+v);
});
JSONObject.parseObject和jsonObject.toJavaObject
它们都可以将json格式字符串转为实体对象,他们的区别是parseObject
直接可以将json格式字符串转为实体对象,而toJavaObject
需要先将json格式字符串转为JSONObject
,然后再toJavaObject
转为实体对象
JSONObject.parseObject
String jsonStr= "{\"name\":\"张三\",\"age\":18}";
Employee emp=JSONObject.parseObject(jsonStr, Employee.class);
JSONObject.toJavaObject
String jsonString = "{\"name\":\"张三\",\"age\":18}";
//JSON格式字符串转为JSONObject
JSONObject jsonObject = JSONObject.parseObject(jsonString);
//JSONObject转为实体对象
User user = jsonObject.toJavaObject(User.class);
HashMap转对象
数据结构
转换后的对象
//先获取hashmap对象
Object loginUser = RpcContext.getContext().getObjectAttachment("loginUser");
//转成字符串
String jsonString = JSON.toJSONString(loginUser);
//转成对象
LoginUser user = JSON.parseObject(jsonString, LoginUser.class);