简体   繁体   English

在Java中使用Jackson进行JSON解析

[英]JSON parsing using Jackson in java

I have a json file which is a complex one, I need to parse/read the below file and display each of its value 我有一个复杂的json文件,我需要解析/读取以下文件并显示其每个值

   {
  "spl": "{\"e\":{\"h\":{\"ak\":\"1c8d1d7eaa32ff3f58a882\",\"at\":\"app\"},\"c\":{\"dt\":\"MEmulator\",\"mi\":\"D56BF292F1FC16F\",\"ui\":\"m4J\/2s=\",\"av\":\"0.2\",\"pn\":\"WP\",\"pv\":\"7.10\",\"nv\":\"C# 1.1.0\",\"al\":\"en\"},\"b\":[{\"ts\":139658547,\"tz\":-400,\"s\":\"StartUpScreen\",\"et\":8,\"ev\":\"sessionStart\",\"si\":\"19477682-de55-414f-82c9-19bec331dc33\",\"tt\":{\"DaySessionStarted\":\"Tuesday\"}},{\"ts\":1319549658751,\"tz\":-400,\"s\":\"StartUpScreen\",\"et\":3,\"ev\":\"AutomaticFeedRefresh\",\"si\":\"19477682-de5ec331dc33\",\"tt\":{}},{\"ts\":1319549675609,\"tz\":-400,\"s\":\"MainScreen\",\"et\":3,\"ev\":\"MainScreen Event\",\"si\":\"19477682-de55-414f-82c9-19bec331dc33\",\"tt\":{}},{\"ts\":1319549677179,\"tz\":-400,\"s\":\"MainScreen\",\"et\":3,\"ev\":\"MainScreen Event\",\"si\":\"19477682-de55-414f-82c9-19bec331dc33\",\"tt\":{}},{\"ts\":1319549678401,\"tz\":-400,\"s\":\"MainScreen\",\"et\":3,\"ev\":\"MainScreen Event\",\"si\":\"19477682-de55-414f-82c9-19bec331dc33\",\"tt\":{}},{\"ts\":1319549679973,\"tz\":-400,\"s\":\"MainScreen\",\"et\":3,\"ev\":\"MainScreen Event\",\"si\":\"19477682-c9-19bec331dc33\",\"tt\":{}}],\"tt\":{\"OSV\":\"ME\"}}}",
  "SP": {
    "httpHeaders": {
      "x-bluecoat-via": [
        "35D3468F4D5F18"
      ],
      "content-type": [
        "application\/x-form-ur"
      ],
      "connection": [
        "Keep-Alive"
      ],
      "host": [
        "20.198.134.198:8080"
      ],
      "accept": [
        "text\/html, image\/gif, image\/jpeg, *; q=.2, *\/*; q=.2"
      ],
      "content-length": [
        "1791"
      ],
      "user-agent": [
        "Java\/1.6.0_23"
      ]
    },
    "senderIp": [
      "112.101.216.113"
    ],
    "receiveTimeStamp": "2012-06-26T06:29:36+0000"
  }
}

I started using Jackson pull API and i found it easier although too much of work... Here is my code: 我开始使用Jackson的pull API,尽管工作量太大,但发现它更容易...这是我的代码:

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.MappingJsonFactory;

import java.io.File;
public class HugeJson1 {
  public static void main(String[] args) throws Exception {
     try {
    JsonFactory f = new MappingJsonFactory();
    JsonParser jp = f.createJsonParser(new File("C:\\json1.json"));
    jp.nextToken();
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldName1 = jp.getCurrentName();
        jp.nextToken();
        if ("spl".equals(fieldName1)) {
            while (jp.nextToken() != JsonToken.END_OBJECT){
                String fieldName2 = jp.getCurrentName();
                jp.nextToken();
                if("e".equals(fieldName2)){
                    while (jp.nextToken() != JsonToken.END_OBJECT) {
                        String fieldName3 = jp.getCurrentName();
                        jp.nextToken();
                        if("h".equals(fieldName3)) {
                            while (jp.nextToken() != JsonToken.END_OBJECT) {
                                String fieldName4 = jp.getCurrentName();
                                jp.nextToken();
                                if("ak".equals(fieldName4)){
                                    jp.nextToken();
                                    System.out.println(jp.getText());
                                    }
                                else if("at".equals(fieldName4)) {
                                    jp.nextToken();
                                    System.out.println(jp.getText());
                                }
                                jp.nextToken();
                                if ("c".equals(fieldName4)){
                                    while (jp.nextToken() != JsonToken.END_OBJECT) {
                                        String fieldName5 = jp.getCurrentName();
                                        jp.nextToken();
                                        if ("dt".equals(fieldName5)){
                                            jp.nextToken();
                                            System.out.println(jp.getText());                                                   
                                        }
                                        else if ("mi".equals(fieldName5)) {
                                            jp.nextToken();
                                            System.out.println(jp.getText());   
                                        }
                                        else if ("ui".equals(fieldName5)) {
                                            jp.nextToken();
                                            System.out.println(jp.getText());   
                                        }
                                        else if ("av".equals(fieldName5)) {
                                            jp.nextToken();
                                            System.out.println(jp.getText());   
                                        }
                                        else if ("pn".equals(fieldName5)) {
                                            jp.nextToken();
                                            System.out.println(jp.getText());   
                                        }
                                        else if ("pv".equals(fieldName5)) {
                                            jp.nextToken();
                                            System.out.println(jp.getText());   
                                        }
                                        else if ("nv".equals(fieldName5)) {
                                            jp.nextToken();
                                            System.out.println(jp.getText());   
                                        }
                                        else if ("al".equals(fieldName5)) {
                                            jp.nextToken();
                                            System.out.println(jp.getText());   
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                }
            }
        }
   }catch (JsonGenerationException e) {

      e.printStackTrace();

 } catch (JsonMappingException e) {

      e.printStackTrace();

 } catch (IOException e) {

      e.printStackTrace();
  }
}

The problem is this code does not show any output, neither it throws exception. 问题是此代码不显示任何输出,也不会引发异常。 Can someone help me finding an issue with this? 有人可以帮我找到问题吗?

Thanks 谢谢

It looks like your JSON isn't well-formed, but that could just be how you've copied and pasted it. 看起来您的JSON格式不正确,但这可能只是您复制和粘贴它的方式。 If I look at it with the human eye, it seems like it ought to be like this: 如果我用肉眼看的话,看起来应该像这样:

{"spl":{"e":{"h":{"ak":"1c8dd5a","at":"app"},"c":{"dt":"Microsoft","mi":"8047DCF627674404AF721B","ui":"obGge3hLBp0rr4J\/2s=","av":"0.2","pn":"WP","pv":"7.7712","nv":"C# 1.0","al":"en"}}}}

In particular the escaped quotes probably shouldn't be escaped, and the quotes around the value of "spl" would turn it into a string instead of a sub-object with its own properties. 特别是转义的引号可能不应转义,“ spl”值周围的引号会将其转换为字符串,而不是具有自己属性的子对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM