1.org中jackson转换json,springboot中内置jackson
ObjectMapper o=new ObjectMapper(); List<>list=new ArrayList(); String jons=o.writeAsValueString(list);
一、Fastion
使用阿里的fastjson
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.83</version> </dependency> import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject;
1.json格式的字符串 转JSONObject
public static void myJson(String str) { JSONObject obj = JSON.parseObject(str); // 测试结果 System.out.println(obj); }
2.json格式的字符串 转JSONArray
public static void myJson(String str) { JSONArray array = JSON.parseArray(str); // 测试结果 for(Object object : array){ JSONObject obj = (JSONObject)object; System.out.println(obj); } }
3.json格式的字符串 转JSONObject、JSONArray
public static void myJson(String str) { JSONObject jsonObj = JSON.parseObject(str); JSONArray array = JSON.parseArray(jsonObj.getString("name")); // 测试结果 for(Object object:array){ JSONObject obj = (JSONObject)object; System.out.println(obj); } }
4.json格式的字符串 转实体类(javaBean)
public class Persion { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public static void myJson(String str) { //如果str 是OBJECT 则: String s = JSONObject.toJSONString(Persion); //再改对象 Persion persion = JSON.parseObject(str, new TypeReference<Persion>(){}); // 测试结果 System.out.println("name:" + persion.getName() + " , " + "age:" + persion.getAge()); }
5.map格式 转JSONObject
public static void myJson(String str) { Map<String, Object> map = new HashMap<String, Object>(); map.put("name","张三"); map.put("age","18"); JSONObject jsonObj=new JSONObject(map); // 测试结果 System.out.println(jsonObj.toString()); }
6.实体类 转json格式的字符串
public static void myJson(Student student) { String str = JSONObject.toJSONString(student); // 测试结果 System.out.println(str); }
7、实体类转实体类
public static void myJson(Student1 student1) { String str = JSONObject.toJSONString(student); Student2 student2 = JSON.parseObject(str, new TypeReference<Student2>(){}); }
一、提取JsonObject(或Map)中的key-value值
1、获取JsonObject 的key
public void myJson(JSONObject params){ Map<String, Object> map = params; Set<String> keys = map.keySet(); for (String key : keys) { System.out.println(key+"-----"+map.get(key)); } }
2.获取JsonObject中的School的key-value值
public void myJson(JSONObject params){ Map<String, Object> map = params; // {"小学":"4","初中":"2","高中":"1"} Map<String, String> m1 = (Map<String, String>) map.get("school"); Set<String> keys = m1.keySet(); for (String key : keys) { System.out.println(key+" ----- "+m1.get(key)); } }
二、Gosn
# 需要转jsonStirng的对象
Gson gson= new GsonBuilder().serializeNulls().create(); String jsonObject = gson.toJson(object);
三、String字符串分隔
解决字符串逗号分隔
String namesStr = "山东省,江苏省,广东省,浙江省"; String[] names = namesStr.split(","); for (int i = 0, len = names.length; i < len; i++) { System.out.println(names[i]) }
2.alibaba中fastjson转换成json
@GetMapping("/test") public TbUser testHttpClient(){ String url="http://localhost:8080/saveUser"; TbUser user = new TbUser(); user.setPhone("13568775074"); user.setUsername("minming"); user.setPassword("123456"); //fastjson的JSONObject将Object转json String s = JSONObject.toJSONString(user); //返回json类型数据 String result = CloseableHttpCilentsUtils.doPost(url, user, null); fastjson的JSONObject将json转Object JSONObject response = JSONObject.parseObject(result); TbUser parse = JSONObject.toJavaObject(response, TbUser.class); return parse; }
返回结果:
{ "password": "123456", "phone": "13568775074", "username": "minming"