FSATJSON工具

maven依赖

1
2
3
4
5
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>

案例编码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//存储数据
public void fun(JSONObject object){
//string
object.put("string","string");

//int
object.put("int",2);

//boolean
object.put("boolean",true);

//array
List<Integer> integers = Arrays.asList(1,2,3);
object.put("list",integers);

//null
object.put("null",null);

System.out.println(object);
}

输出结果:
{"boolean":true,"string":"string","list":[1,2,3],"int":2}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//解析数据
public void fun(JSONObject object){
//string
String s = object.getString("string");
System.out.println(s);

//int
int i = object.getIntValue("int");
System.out.println(i);

//boolean
boolean b = object.getBooleanValue("boolean");
System.out.println(b);

//list
List<Integer> integers = JSON.parseArray(object.getJSONArray("list").toJSONString(),Integer.class);
integers.forEach(System.out::println);
//null
System.out.println(object.getString("null"));
}

----\(˙<>˙)/----赞赏一下吧~