alibaba FastJson 使用

alibaba fastjson api 使用

fastjson的常用api:
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray

public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject

public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean

public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray

public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合

public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本

public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本

public static final Object toJSON(Object javaObject); //将JavaBean转换为JSONObject或者JSONArray。


使用maven导入依赖jar包:

1 <!--阿里巴巴 fastjson依赖-->
2 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
3 <dependency>
4 <groupId>com.alibaba</groupId>
5 <artifactId>fastjson</artifactId>
6 <version>1.2.47</version>
7 </dependency>

直接测试:

 1 import com.alibaba.fastjson.JSONArray;
 2 import com.alibaba.fastjson.JSONObject;
 3 import org.junit.Test;
 4 
 5 import java.util.Arrays;
 6 import java.util.List;
 7 
 8 public class FastJsonTest {
 9 
10 @Test
11 public void test () {
12 // 1.测试将一个【对象】转换成【json字符串】
13 String jsonString = JSONObject.toJSONString(new Person("测试1",18));
14 System.out.println(jsonString);
15 
16 // 2.测试将一个【json字符串】转换成【对象】
17 Person person1 = JSONObject.parseObject(jsonString, Person.class);
18 System.out.println(person1);
19 
20 // 3.测试将【多个对象】转换成【json字符串数组】
21 String jsonString1 = JSONArray.toJSONString(Arrays.asList(new Person("测试2",19), new Person("测试3", 20)));
22 System.out.println(jsonString1);
23 
24 // 4.测试将【josn字符串数组】转换成多个对象
25 List<Person> people = JSONArray.parseArray(jsonString1, Person.class);
26 System.out.println(people);
27 
28 }
29 }

Person类

 1 public class Person {
 2 private String name;
 3 private Integer age;
 4 
 5 public String getName() {
 6 return name;
 7 }
 8 
 9 public void setName(String name) {
10 this.name = name;
11 }
12 
13 public Integer getAge() {
14 return age;
15 }
16 
17 public void setAge(Integer age) {
18 this.age = age;
19 }
20 
21 public Person(){}
22 
23 public Person(String name,Integer age){
24 this.name=name;
25 this.age=age;
26 }
27 
28 @Override
29 public String toString() {
30 return "Person{" +
31 "name='" + name + '\'' +
32 ", age=" + age +
33 '}';
34 }
35 }

测试结果:

 JSONPath

 1  String jsonStr = "{\n" +
 2                 "    \"store\": {\n" +
 3                 "        \"bicycle\": {\n" +
 4                 "            \"color\": \"red\",\n" +
 5                 "            \"price\": 19.95\n" +
 6                 "        },\n" +
 7                 "        \"book\": [\n" +
 8                 "            {\n" +
 9                 "                \"author\": \"刘慈欣\",\n" +
10                 "                \"price\": 8.95,\n" +
11                 "                \"category\": \"科幻\",\n" +
12                 "                \"title\": \"三体\"\n" +
13                 "            },\n" +
14                 "            {\n" +
15                 "                \"author\": \"itguang\",\n" +
16                 "                \"price\": 12.99,\n" +
17                 "                \"category\": \"编程语言\",\n" +
18                 "                \"title\": \"go语言实战\"\n" +
19                 "            }\n" +
20                 "        ]\n" +
21                 "    }\n" +
22                 "}";
23  System.out.println(JSON.isValid(jsonStr));//判断是否为json
24  //第一本书title
25 String title = (String) JSONPath.read(jsonStr, "store.book[0].title");
26 System.out.println(title);
27   //多层结构 相对路径
28 List<String> prices2 =  (List<String>) JSONPath.read(jsonStr, "..book[price>10].price");
29 //从返回列表中取元素
30 System.out.println(JSONPath.read(jsonStr, "$.store.book.price[0,1]"));
31  //category(类别)为科幻的book 
32 List<Book> list3 = (List<Book>) JSONPath.read(jsonStr,"$.store.book[category = '科幻']");
33 //bicycle的所有属性值,,只取value
34         JSONObject jsonObject = JSON.parseObject(jsonStr);
35         Collection<String> values = (Collection<String>) JSONPath.eval(jsonObject, "$.store.bicycle.*");
36         System.out.println(values);
37         System.out.println(JSONPath.read(jsonStr, "$.store.bicycle.*"));

 

posted @ 2020-04-02 17:37  铜锣烧0901  阅读(206)  评论(0编辑  收藏  举报