1.对象与字符串之间的互转
//将对象转换成为字符串
String str = JSON.toJSONString(infoDo);
//字符串转换成为对象
InfoDo infoDo = JSON.parseObject(strInfoDo, InfoDo.class);
2.对象集合与字符串之间的互转
//将对象集合转换成为字符串
String users = JSON.toJSONString(users);
//将json字符串转成对象
User user= JSON.parseObject(users,User.class);
//map转对象
User user= JSON.parseObject(JSONObject.toJSONString(map),User.class);
// 将对象转成JsonObject
JSONObject jsonStu = (JSONObject) JSONObject.toJSON(user);
//将字符串转换成为对象集合
List<User> userList = JSON.parseArray(userStr, User.class);
3.字符串互转JSONObject
//String 转 Json对象
JSONObject jsonObject = JSONObject.parseObject(jsonString);
//String 转 JSONArray 对象
JSONArray jsonObject3= JSONArray.parseArray(jsonString);
//json对象转string
JSONObject jsonObject = JSONObject.parseObject(str);//json对象转字符串
String jsonString = jsonObject.toJSONString();
4.map与字符串之间互转
//字符串转map
JSONObject jsonObject = JSONObject.parseObject(str);
Map<String,Object> map = (Map<String,Object>)jsonObject;// //json对象转Map
//map转字符串
String jsonString = JSON.toJSONString(map);
5.Map 转 Json对象互转
//map转json对象
Map<String,Object> map = new HashMap<>();
map.put("age", 24);
map.put("name", "cool_summer_moon");
JSONObject json = new JSONObject(map);
//json对象转Map
Map<String,Object> map = (Map<String,Object>)jsonObject;
6.创建json对象
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "Jason");
jsonObject.put("id", 1);
jsonObject.put("phone", "18271415782");
System.out.println(jsonObject.toString());
7.List和JSONArray互转
//1.fastjson List转JSONArray
List<T> list = new ArrayList<T>();
JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
//2.fastjson JSONArray转List
JSONArray array = new JSONArray();
List<EventColAttr> list = JSONObject.parseArray(array.toJSONString(), EventColAttr.class);
//3.fastjson 字符串转List
String str = "";
List<T> list = JSONObject.parseArray(str,T.class);
8.判断是否存在key
jsonObject.containsKey("key")
9.遍历JSONObject /map
JSONObject response = JSONObject.parseObject(result);
for (Map.Entry<String, Object> entry : response.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
// 传统的Map迭代方式
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
// JDK8的迭代方式
map.forEach((key, value) -> {
System.out.println(key + ":" + value);
});
10.Gson
//解成对象
Fromat mFromat = new Gson().fromJson(jsonStringObject, Fromat.class);
//解成对象组
LinkedList<Fromat> list = new LinkedList<MainActivity.Fromat>();
Type type = new TypeToken<LinkedList<Fromat>>(){}.getType();
list = new Gson().fromJson(jsonStringArray, type);
//泛型统一封装时 需要传个 type 进来
new TypeToken<LinkedList<Fromat>>(){}.getType();
fromJson(String json, Type typeOfT)
public <T> T fromJson(String json, Type typeOfT)
//解成对象
Fromat mFromat = new Gson().fromJson(jsonStringObject, Fromat.class);
//解成对象组
LinkedList<Fromat> list = new LinkedList<MainActivity.Fromat>();
Type type = new TypeToken<LinkedList<Fromat>>(){}.getType();
list = new Gson().fromJson(jsonStringArray, type);
//泛型统一封装时 需要传个 type 进来
new TypeToken<LinkedList<Fromat>>(){}.getType();
fromJson(String json, Type typeOfT)
public <T> T fromJson(String json, Type typeOfT)
//json转map
Gson gson = new Gson();
Map<String,String> map = new HashMap<>();
JSONObject strJson = JSONObject.fromObject(data);
map= gson.fromJson(strJson.toString(), map.getClass());