简体   繁体   中英

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

   {
  "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:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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