0

我有一个复杂的 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"
  }
}

我开始使用 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();
  }
}

问题是这段代码没有显示任何输出,也没有抛出异常。有人可以帮我找到这个问题吗?

谢谢

4

1 回答 1

0

看起来您的 JSON 格式不正确,但这可能只是您复制和粘贴它的方式。如果我用肉眼看它,它似乎应该是这样的:

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

特别是转义的引号可能不应该被转义,并且“spl”值周围的引号会将其变成字符串而不是具有自己属性的子对象。

于 2012-08-13T18:35:56.250 回答