fastjson使用(一) -- 最常用的功能[构造和解析JSON]

在实际的开发过程中,很多时候数据传递,我们使用的JSON,这个时候,构造和解析JSON成了常用的功能了,JAVA实际开发中,网上有很多的JSON的类库,比如:json-lib、jackson、fastjson等等。而我今天要谈的是fastjson的使用。fastjson是阿里的开源的java的JSON类库。

1.构造JSON格式的数据

由于代码比较简单,就直接上代码:

<span style="font-size:12px;">JSONObject object = new JSONObject();
object.put("key1", "value1"); // String
object.put("key2", 1); // int
object.put("key3", 35.7); // double
object.put("numberString", "55.8");
Map<String, Object> map = new HashMap<String, Object>();
map.put("map_key1", "map_value1");
map.put("map_key2", "map_value2");
object.put("key4", map); <span style="color:#3333FF;">// 如果是Map,则添加里层JSONObject</span>
object.putAll(map); <span style="color:#3333FF;">// 如果是通过putAll,则会循环依次作为Object对象插入JSONObject数据</span>
User user = new User();
user.setAge(18);
user.setName("张三");
<span style="color:#3333FF;">/*
 *  如果是实体类对象,则会将实体类组装成JSONObject放进去
 */</span>
object.put("user", user);
List<User> users = new ArrayList<>();
for (int i = 0; i < 3; i++) {
    User u = new User();
    u.setAge(i + 18);
    u.setSex(i % 2 == 0 ? "男" : "女");
    users.add(u);
}
object.put("list", users); // List
// 构造JSONArray格式的数据
JSONArray array = new JSONArray();
array.add("d");
object.put("array", array);</span>
查看构造的JSON数据格式:

<span style="font-size:12px;">JSONObject : {"key4":{"map_key1":"map_value1","map_key2":"map_value2"},"key3":35.7,"map_key1":"map_value1","key2":1,"map_key2":"map_value2","numberString":"55.8","key1":"value1","list":[{"age":18,"date":"20160104","gender":"男"},{"age":19,"date":"20160104","gender":"女"},{"age":20,"date":"20160104","gender":"男"}],"user":{"age":18,"date":"20160104","name":"张三"},"array":["d"]}</span>

2.解析JSON格式的数据

就以上面创建的JSON作为源数据进行解析:

JSONObject object = new JSONObject(source); // 测试创建JSONObject
System.out.println("JSONObject : " + object.toJSONString());
// key4
JSONObject key4Object = object.getJSONObject("key4");
System.out.println("map_key1 value " + key4Object.getString("map_key1"));
System.out.println("map_key2 value " + key4Object.getString("map_key2"));
// key3
System.out.println("key3 value " + object.getDoubleValue("key3"));
System.out.println("key3 value " + object.getDouble("key3"));
System.out.println("key3 value " + object.getString("key3"));
// map_key1
System.out.println("map_key1 value " + object.getString("map_key1"));
// key2
System.out.println("key2 value " + object.getInteger("key2"));
System.out.println("key2 value " + object.getIntValue("key2"));
System.out.println("key2 value " + object.getDouble("key2"));
System.out.println("key2 value " + object.getDoubleValue("key2"));
System.out.println("key2 value " + object.getString("key2"));
// numberString
System.out.println("numberString value " + object.getString("numberString"));
System.out.println("numberString value " + object.getDouble("numberString"));
// user
JSONObject userObject = object.getJSONObject("user");
System.out.println("user : " + userObject.toJSONString());
User user = JSON.toJavaObject(userObject, User.class);
user.setSex("男");
System.out.println(user.toString());
// JavaBean --> String
String userString = JSON.toJSONString(user);
System.out.println("userString " + userString);
// list
JSONArray array = object.getJSONArray("list");
for (int i = 0; i < array.size(); i++) {
    User user2 = JSON.toJavaObject(array.getJSONObject(i), User.class);
    System.out.println(user2.toString());
}
// 解析失败
System.out.println("not exit : " + object.getInteger("integer")); // null
System.out.println("not exit : " + object.getIntValue("integer")); // 0

注意:

    1.如果存入的String类型的数字,在解析的时候同样可以通过getDouble()的方式获取,会自动转换为double格式

    2.getXX()方式解析的时候,如果解析失败会返回null,而通过getXXValue()方式解析的时候,如果解析失败,会返回对应方式的默认值,比如:getIntValue() --> 0; getDoubleValue() --> 0.0; ……


你可能感兴趣的:(java,json,fastjson)