FastJson的使用
1.在pom中引入fastjson依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>x.x.x</version>
</dependency>
2.使用fastjson
fastjson的使用主要有三个对象
- JSON
- JSONObject
- JSONArray
JSONObject和JSONArray继承自JSON
2.1JSON对象
JSON这个类主要用于转换:
- 将java对象序列化为json字符串
- 将JSON字符串反序列化为java对象
这三个方法用的比较多:
parseObject(String text, Class<T> clazz)
parseArray(String text, Class<T> clazz)
toJSONString(Object object)
2.2JSONObject
JSONObject继承了JSON,实现了Map接口,跟使用Map没有太多的区别,常用方法
getString(String key)
put(String key,Object value)
remove(Object key)
2.3 JSONArray
JSONArray是JSON数组,JSON数组对象中存储的是一个个JSON对象,所以类中的方法主要用于直接操作JSON对象,常用方法
getJSONObject(int index)
add(Object e)
3.fastjson常用方式实例
3.1Map转JSON字符串
public void MaptoJson(){
HashMap<String, String> map = new HashMap<>();
map.put("key1","1");
map.put("key2","2");
String s = JSON.toJSONString(map);
System.out.println("Map转Json:"+s);
}
3.2JSONObject
public void JSONObject(){
JSONObject jsonObject = new JSONObject();
List<Object> array = new ArrayList<>();
array.add("111111");
sonObject.put("key1","1");
jsonObject.put("key2","2");
jsonObject.put("array",array);
jsonObject.toString();
System.out.println("测试JSONObjet:"+jsonOb
}
3.3POJO List转JSON字符串
POJO一般叫做简单的Java对象。
public void test6() {
Person person1 = new Person();
person1.setName("张三");
person1.setAge(28);
person1.setBirthday(new Date());
Person person2 = new Person();
person2.setName("李四");
person2.setAge(25);
person2.setBirthday(new Date());
List<Person> persons = new ArrayList<Person>();
persons.add(person1);
persons.add(person2);
String object = JSON.toJSONString(persons);
System.out.println(object);
}
3.4Json字符串转JsonObject
@Test
public void JsonToJsonObject() {
String jsonStr = "{\"key1\":\"One\",\"key2\":\"110\"}";
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
System.out.println(jsonObject.getString("key1"));
System.out.println(jsonObject.getInteger("key2"));
System.out.println(jsonObject);
}
3.5JsonObject转Json字符串
public void test3() {
String jsonStr = "{\"key1\":\"One\",\"key2\":\"110\"}";
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
System.out.println(jsonObject.getString("key1"));
System.out.println(jsonObject.getInteger("key2"));
String parserJsonStr = JSONObject.toJSONString(jsonObject);
System.out.println(parserJsonStr);
}
3.6JSONArray 添加 JSONObject
@Test
public void JsonObjectInJsonArray() {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("name", "张三");
jsonObject1.put("age", 25);
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("name", "李四");
jsonObject2.put("age", 28);
JSONArray jsonArray = new JSONArray();
jsonArray.add(jsonObject1);
jsonArray.add(jsonObject2);
String jsonArrStr = JSONArray.toJSONString(jsonArray);
System.out.println(jsonArrStr);
}
3.7Json数组字符串转JsonArray
@Test
public void jsonStrToJsonArray() {
String jsonArrStr = "[{\"name\":\"张三\",\"age\":25},{\"name\":\"李四\",\"age\":28}]";
JSONArray jsonArray = JSONArray.parseArray(jsonArrStr);
for (Object object : jsonArray) {
JSONObject jsonObject = (JSONObject) object;
System.out.println(jsonObject.getString("name"));
System.out.println(jsonObject.getString("age"));
}
}
3.8POJO转JsonStr
@Test
public void PojoToJsonStr() {
Person person1 = new Person();
person1.setName("张三");
person1.setAge(26);
person1.setBirthday(new Date());
String object = JSONObject.toJSONString(person1);
System.out.println(object);
}
3.9 POJO 转 JSONObject
@Test
public void PojoToJsonObject{
Person person1 = new Person();
person1.setName("张三");
person1.setAge(28);
person1.setBirthday(new Date());
/**方式一*/
String jsonStr = JSONObject.toJSONString(person1);
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
System.out.println(jsonObject.get("name"));
/**方式二*/
JSONObject jsonObject1 = (JSONObject)JSONObject.toJSON(person1);
System.out.println(jsonObject1.get("age"));
}
3.10PojoList转JsonArray
@Test
public void test9() {
Person person1 = new Person();
person1.setName("张三");
person1.setAge(28);
person1.setBirthday(new Date());
Person person2 = new Person();
person2.setName("李四");
person2.setAge(25);
person2.setBirthday(new Date());
List<Person> persons = new ArrayList<Person>();
persons.add(person1);
persons.add(person2);
/**方式1*/
String jsonArrStr = JSONArray.toJSONString(persons);
JSONArray jsonArray = JSONArray.parseArray(jsonArrStr);
JSONObject jsonObject1 = (JSONObject)jsonArray.get(0);
System.out.println(jsonObject1.get("name"));
System.out.println(jsonArray);
/**方式2*/
JSONArray jsonArray1 = (JSONArray)JSONArray.toJSON(persons);
JSONObject jsonObject2 = (JSONObject)jsonArray1.get(1);
System.out.println(jsonObject2.get("name"));
}
3.11JSON字符串 转 Map 与 POJO
@Test
public void JsonToMap() {
String content = "{\"PARENT_ID\":1,\"NAME\":\"国家级正职\",\"CODE\":\"101\",\"LEVEL_NUM\":2,\"ID\":2}";
Map map = JSONObject.parseObject(content, Map.class);//json 对象转 map
System.out.println(map);//输出:{CODE=101, LEVEL_NUM=2, ID=2, PARENT_ID=1, NAME=国家级正职}
}
3.12将json对象转JavaBean
@Test
public void jsonToJavaBean() {
Person person1 = new Person();
person1.setName("张三");
person1.setAge(28);
person1.setBirthday(new Date());
String jsonPOJOStr = JSON.toJSONString(person1);
Person person = JSONObject.parseObject(jsonPOJOStr, Person.class);
System.out.println(person);
}
3.13Json字符串转MapList
@Test
public void JsonToList() {
String content = "[{\"PARENT_ID\":1,\"NAME\":\"国家级正职\",\"CODE\":\"101\",\"LEVEL_NUM\":2,\"ID\":2},{\"PARENT_ID\":1,\"NAME\":\"国家级副职\",\"CODE\":\"102\",\"LEVEL_NUM\":2,\"ID\":3}]";
//parseArray(String text, Class<T> clazz):clazz 指定 list 中的元素类型
List<Map> mapList = JSONArray.parseArray(content, Map.class);//json 转 List<Map>
System.out.println(mapList);
}
3.14Json字符串转POJOList
@Test
public void JsonToPersonList() {
String jsonArrPOJOStr = "[{\"birthday\":1530512954968,\"name\":\"张三\",\"age\":28}," +
"{\"birthday\":1530512954968,\"name\":\"李四\",\"age\":25}]";
List<Person> personList = JSONArray.parseArray(jsonArrPOJOStr, Person.class);
for (Person person : personList) {
System.out.println(person);
}