jackson的简单实用实例(json)

一个json格式的字符串比如:

{"status":10001,"code":"HDkGzI","pubkey":"DBCEEECFD3F6808C85254B1","servertime":1475741518}

,在java中对应的字符串为:

"{"status":10001,"code":"HDkGzI","pubkey":"DBCEEECFD3F6808C85254B1","servertime":1475741518}"

格式化后:

{
"status": 10001,
"code": "HDkGzI",
"pubkey": "DBCEEECFD3F6808C85254B1",
"servertime": 1475741518
}

有的时候我们想在这个json格式字符串中去一项,比如想去出pubkey这一项,

那么结合jackson这个工具就可以很方便地解决这个问题。

代码如下:

 1 package com.test.javaAPI.json;
 2 
 3 import java.io.IOException;
 4 import java.util.Map;
 5 
 6 import org.codehaus.jackson.JsonParseException;
 7 import org.codehaus.jackson.map.JsonMappingException;
 8 import org.codehaus.jackson.map.ObjectMapper;
 9 import org.junit.Test;
10 
11 import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
12 
13 public class JacksonTest2 {
14     public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
15         String str = new JacksonTest2().removeJsonObjByKey(UtilJackson.jsonStr4, UtilJackson.jsonStr4_KEY1);
16         System.out.println(str);
17     }
18 
19     /**
20      * 根据键除去json格式字符串中的某一个键值对
21      * 
22      * @param jsonStr
23      * @param key
24      * @return
25      * @throws IOException
26      * @throws JsonMappingException
27      * @throws JsonParseException
28      */
29     public String removeJsonObjByKey(String jsonStr, String key)
30             throws JsonParseException, JsonMappingException, IOException {
31         ObjectMapper objMap = new ObjectMapper();
32         // 1 把json格式字符串转换为 java.util.Map
33         Map map = objMap.readValue(jsonStr, Map.class);
34         // 2 删除map中的对应key的项目
35         map.remove(key);
36         // 准备字节流,接收ObjectMapper中写出的输出流
37         ByteOutputStream bops = new ByteOutputStream();
38         // 3 把map重新转换为json格式字符串
39         objMap.writeValue(bops, map);
40         if (!"".equals(bops)) {
41             return bops.toString();
42         }
43         return "";
44     }
45 
46     /**
47      * 方法的作用:去除一个json格式字符串的某一个key 删除 这个json字符串里的这个key对应的对象 该方法与框架中的 String
48      * cn.sinobest.framework.web.his.JsonManager.removeDataPackage(String
49      * jsonStr) 这个方法的功能一致
50      * 
51      * @param jsonKey
52      * @return
53      * @throws JsonParseException
54      * @throws JsonMappingException
55      * @throws IOException
56      */
57     @Test
58     public String removeDataPackage(String jsonKey) throws JsonParseException, JsonMappingException, IOException {
59         ObjectMapper objMap = new ObjectMapper();
60         Map map = objMap.readValue(UtilJackson.jsonStr_HN, Map.class);
61         // map.remove("DataPackage");
62         map.remove(jsonKey);
63         ByteOutputStream bops = new ByteOutputStream();
64         objMap.writeValue(bops, map);
65         System.out.println(bops.toString());
66         return null;
67     }
68 
69 }

其中main方法字符串分别为:

1 public static String jsonStr4 = "{"verified":false,"name":{"last":"Hankcs","first":"Joe"},"userImage":"Rm9vYmFyIQ==","gender":"MALE"}";
2     public static String jsonStr4_KEY1 = "verified";

运行后的结果为:

         {"name":{"last":"Hankcs","first":"Joe"},"userImage":"Rm9vYmFyIQ==","gender":"MALE"},

而原来的字符串jsonStr4为:

{"verified":false,"name":{"last":"Hankcs","first":"Joe"},"userImage":"Rm9vYmFyIQ==","gender":"MALE"}。

原文地址:https://www.cnblogs.com/Sunnor/p/5934036.html