在Jackson中使用JsonParser.nextToken时,如何将值读为JSON

时间:2013-02-07 21:16:06

标签: java json jackson

我正在从树模型转换为Jackson内的流式api,以防止在Android上出现堆内存问题。我想通过将整个“Tags”值数组作为JSON字符串读取来进一步加快处理速度。在此示例中,我想要一个JSON字符串,其中包含每个报告ID的所有三个标记及其值。

我的数据的简化版本是:

    "TagRpts":[
   {  "ID":46,
      "Tags":[
         {  "Name":"Tag1",
            "Values":[
                {"t":"Instance 1","v":"407"},
                {"t":"Instance 2","v":"360"},
                {"t":"Instance 3","v":"309"}]},
         {  "Name":"Tag2",
            "Values":[
                {"t":"Instance 1","v":"926"},
                {"t":"Instance 2","v":"168"},
                {"t":"Instance 3","v":"366"}]},
         {  "Name":"Tag3",
            "Values":[
                {"t":"Instance 1","v":"744"},
                {"t":"Instance 2","v":"668"},
                {"t":"Instance 3","v":"62"}]}]},
   {  "ID":47,
      "Tags":[
         {  "Name":"machine 1",
            "Values":[
                {"t":"Instance 1","v":"613"},
                {"t":"Instance 2","v":"882"},
                "t":"Instance 3","v":"602"}]},
         {  "Name":"machine 2",
            "Values":[
                {"t":"Instance 1","v":"38"},
                {"t":"Instance 2","v":"38"},
                {"t":"Instance 3","v":"329"}]},
         {  "Name":"machine 3",
            "Values":[
                {"t":"Instance 1","v":"276"},
                {"t":"Instance 2","v":"721"},
                {"t":"Instance 3","v":"660"}]}]}
]

我目前正在使用此代码阅读它(为了说明而简化 - 删除了将逗号处理到字符串构建器中的区域等):

if ("TagRpts".equals(fieldname)) { // TagRpts 
    jParser.nextToken(); // [ or null
    if (jParser.getText() != "null") {
        while (jParser.nextToken() != JsonToken.END_ARRAY) { // Tag report Array
            while (jParser.nextToken() != JsonToken.END_OBJECT) {
                fieldname = jParser.getCurrentName();

                if ("ID".equals(fieldname)) {
                    jParser.nextToken();
                    aTagReport.ID = jParser.getText();}

                if ("Tags".equals(fieldname)) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("[");

                    while(jParser.nextToken() != JsonToken.END_ARRAY) {
                        fieldname = jParser.getCurrentName();

                        if ("Name".equals(fieldname)) {
                            jParser.nextToken();
                            sb.append("{\"Name\":\"" + Utility.encodeJSON(jParser.getText()) + "\"");}

                        if ("Values".equals(fieldname)) {
                            sb.append("\"Values\":[");

                            while(jParser.nextToken() != JsonToken.END_ARRAY) {
                                fieldname = jParser.getCurrentName();

                                if ("t".equals(fieldname)) {
                                    jParser.nextToken();
                                    sb.append("{\"t\":\"" + Utility.encodeJSON(jParser.getText()) + "\"");}

                                if ("v".equals(fieldname)) {
                                    jParser.nextToken();
                                    sb.append(",\"v\":\"" + Utility.encodeJSON(jParser.getText()) + "\"}");}
                            }
                            sb.append("]}");
                        } 
                    } 
                    sb.append("]");
                    aTagReport.Tags = sb.toString();
                } 
            } 
            aTagReport.SaveTagReport();
            aTagReport = new TagReport();
        }
    }
} 

1 个答案:

答案 0 :(得分:0)

为什么不用JsonParser.readValueAsTree()来阅读子树?然后使用ObjectMapper.writeValueAsString(rootNode)重新创建JSON。