标签:oid 项目 test googl 更新 XML google void fastjson
在 Maven 构建的项目中,在 pom.xml 文件中加入以下依赖即可。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
从 Java 变量到 JSON 格式的编码过程如下:
public void testJson() {
JSONObject object = new JSONObject();
//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}
从 JSON 对象到 Java 变量的解码过程如下:
public void testJson2() {
?
JSONObject object = JSONObject
.parseObject("{\"boolean\":true,\"string\":\"string\",\"list\":[1,2,3],\"int\":2}");
//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"));
?
}
运行结果
string
2
true
1
2
3
null
标签:oid 项目 test googl 更新 XML google void fastjson
原文地址:https://www.cnblogs.com/pbluesky/p/12700783.html