fastjson常用方法实例

Map转JSON字符串

1
2
3
4
5
6
7
8
9
10
11
/**
* 将Map转Json字符串
*/
@Test
public void test1() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("key1", "One");
map.put("key2", "Two");
String mapJson = JSON.toJSONString(map);
System.out.println(mapJson);//输出:{"key1":"One","key2":"Two"}
}

POJO List转JSON字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 将Java Bean List转Json字符串
*/
@Test
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);
/**输出如下:
* [{"age":28,"birthday":1530511546991,"name":"张三"},
{"age":25,"birthday":1530511546991,"name":"李四"}]
*/
}

JSON字符串转JsonObject

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 将Json字符串转为JsonObject对象
* 取值不存在时,返回null,使用Gson会抛异常
*/
@Test
public void test2() {
String jsonStr = "{\"key1\":\"One\",\"key2\":\"110\"}";
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
System.out.println(jsonObject.getString("key1"));//输出one
System.out.println(jsonObject.getInteger("key2"));//输出110
System.out.println(jsonObject.getString("key3"));//输出null
}

JsonObject转Json字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 将JsonObject对象转为Json字符串
* 取值不存在时,返回null,使用Gson会抛异常
*/
@Test
public void test3() {
String jsonStr = "{\"key1\":\"One\",\"key2\":\"110\"}";
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
System.out.println(jsonObject.getString("key1"));//输出:one
System.out.println(jsonObject.getInteger("key2"));//输出:110
System.out.println(jsonObject.getString("key3"));//输出:null
String parserJsonStr = JSONObject.toJSONString(jsonObject);
System.out.println(parserJsonStr);//输出:{"key1":"One","key2":"110"}
}

JsonArray添加JsonObject

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 将JsonObject添加到JsonArray
*/
@Test
public void test4() {
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);//输出:[{"name":"张三","age":25},{"name":"李四","age":28}]
}

Json数组字符串转JsonArray

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 将Json数组字符串转JsonArray
*/
@Test
public void test5() {
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"));
System.out.println("--------------");
}
}

POJO转Json字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 将Java Bean 转Json字符串
*/
@Test
public void test7_2() {
Person person1 = new Person();
person1.setName("张三");
person1.setAge(26);
person1.setBirthday(new Date());

/**两种方式都行
* 因为JSONObject继承了JSON*/
String object = JSONObject.toJSONString(person1);
/*String object = JSON.toJSONString(person1);*/

System.out.println(object);
/**输出如下:
* {"age":26,"birthday":1530511790302,"name":"张三"}
*/
}

POJO转JsonObject

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 将Java Bean 转Json 对象
*/
@Test
public void test8() {
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"));//输出:28
}

POJO List转JsonArray

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 将Java Bean List 转Json 数组
*/
@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"));//输出:张三

/**方式2*/
JSONArray jsonArray1 = (JSONArray)JSONArray.toJSON(persons);
JSONObject jsonObject2 = (JSONObject)jsonArray1.get(1);
System.out.println(jsonObject2.get("name"));//输出:李四
}

JSON字符串转 Map 与 POJO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Test
public void test1() {
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=国家级正职}
}
/**
* 将Json 对象 转 Java Bean
*/
@Test
public void test10() {
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);
/**
* 输出:Person{age=28, name='张三', birthday=Mon Jul 02 14:27:53 CST 2018}
*/
}

字符串Json数组转List

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Test
public void test2() {
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);
}
/**
* 将Json 数组 转 Java List
*/
@Test
public void test11() {
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);
}
/**
* 输出:
* Person{age=28, name='张三', birthday=Mon Jul 02 14:29:14 CST 2018}
* Person{age=25, name='李四', birthday=Mon Jul 02 14:29:14 CST 2018}
*/
}