使用Jackson Json解析器递归解析json文件

时间:2022-09-19 18:04:11

I'm trying to recursively parse a sample Json file that has many sets of complex elements. And the code that i'm trying is this :

我试图以递归方式解析具有许多复杂元素集的示例Json文件。而我正在尝试的代码是这样的:

public class Jsonex {
    public static void main(String argv[]) {
        try {
            Jsonex jsonExample = new Jsonex();
           jsonExample.testJackson();
        } catch (Exception e){
            System.out.println("Exception " + e);
        }       
    }
    public static void testJackson() throws IOException {       
        JsonFactory factory = new JsonFactory();
       // System.out.println("hello");
        ObjectMapper mapper = new ObjectMapper(factory);
        File from = new File("D://albumList.txt");
        TypeReference<HashMap<String,Object>> typeRef = new TypeReference<HashMap<String,Object>>() {};
        HashMap<String,Object> o= mapper.readValue(from, typeRef);
       // System.out.println("" + o);
        Iterator it = o.entrySet().iterator();
       while (it.hasNext()) {

          Map.Entry pairs = (Map.Entry)it.next();
            System.out.println(pairs.getKey() + " = " + pairs.getValue());

           HashMap<String,Object> o1=mapper.readValue(pairs.getValue().toString(),typeRef);
          System.out.println("hey"+o1);
           Iterator it1 = o1.entrySet().iterator();
           while (it1.hasNext()) {
                Map.Entry pairs1 = (Map.Entry)it.next();
                System.out.println(pairs1.getKey() + " = " + pairs1.getValue());
            it1.remove(); // avoids a ConcurrentModificat



    }   
    }
}}

and i get this exception :

我得到这个例外:

Exception org.codehaus.jackson.JsonParseException: Unexpected character ('i' (code 105)): was expecting double-quote to start field name at [Source: java.io.StringReader@2de7753a; line: 1, column: 3]

异常org.codehaus.jackson.JsonParseException:意外的字符('i'(代码105)):期望双引号在[来源:java.io.StringReader@2de7753a; line:1,column:3]

Actually what im trying to do is, parse the file and get list of name object pairs, and take the object which inturn has name-object pairs. - but the problem is that the parser is expecting "" before strings !

实际上我想要做的是,解析文件并获取名称对象对列表,并获取inturn具有名称 - 对象对的对象。 - 但问题是解析器在字符串之前期待“”!

2 个解决方案

#1


5  

Instead of parsing everything by yourself you should consider to use Jacksons built-in tree model feature (http://wiki.fasterxml.com/JacksonTreeModel):

您应该考虑使用Jacksons内置树模型功能(http://wiki.fasterxml.com/JacksonTreeModel),而不是自己解析所有内容:

ObjectMapper mapper = new ObjectMapper(factory);
File from = new File("D://albumList.txt");
JsonNode rootNode = mapper.readTree(from);  

Iterator<Map.Entry<String,JsonNode>> fields = rootNode.fields();
while (fields.hasNext()) {

    Map.Entry<String,JsonNode> field = fields.next();
    System.out.println(field.getKey() + " = " + field.getValue());
    …

}

This should be more convenient in the long run. Have a look at the API at http://fasterxml.github.com/jackson-databind/javadoc/2.1.0/com/fasterxml/jackson/databind/JsonNode.html.

从长远来看,这应该更方便。请访问http://fasterxml.github.com/jackson-databind/javadoc/2.1.0/com/fasterxml/jackson/databind/JsonNode.html查看API。

#2


4  

Just a comment. As you know there are 3 major processing modes that Jackson supports (Data Binding, Streaming API and Tree Model). You need to take into account that if you decide to use the Tree Model, acording to the official docs, the memory usage is proportional to content mapped (similar to data binding), so tree models can NOT be used with huge Json content, unless mapping is done chunk at a time. This is the same problem that data binding encounters; and sometimes the solution is to use Stream-of-Events instead.

只是评论。如您所知,Jackson支持3种主要处理模式(数据绑定,流API和树模型)。您需要考虑到,如果您决定使用树模型,根据官方文档,内存使用量与映射的内容成比例(类似于数据绑定),因此树模型不能与巨大的Json内容一起使用,除非映射一次完成。这与数据绑定遇到的问题相同;有时候解决方案就是使用Stream-of-Events。

#1


5  

Instead of parsing everything by yourself you should consider to use Jacksons built-in tree model feature (http://wiki.fasterxml.com/JacksonTreeModel):

您应该考虑使用Jacksons内置树模型功能(http://wiki.fasterxml.com/JacksonTreeModel),而不是自己解析所有内容:

ObjectMapper mapper = new ObjectMapper(factory);
File from = new File("D://albumList.txt");
JsonNode rootNode = mapper.readTree(from);  

Iterator<Map.Entry<String,JsonNode>> fields = rootNode.fields();
while (fields.hasNext()) {

    Map.Entry<String,JsonNode> field = fields.next();
    System.out.println(field.getKey() + " = " + field.getValue());
    …

}

This should be more convenient in the long run. Have a look at the API at http://fasterxml.github.com/jackson-databind/javadoc/2.1.0/com/fasterxml/jackson/databind/JsonNode.html.

从长远来看,这应该更方便。请访问http://fasterxml.github.com/jackson-databind/javadoc/2.1.0/com/fasterxml/jackson/databind/JsonNode.html查看API。

#2


4  

Just a comment. As you know there are 3 major processing modes that Jackson supports (Data Binding, Streaming API and Tree Model). You need to take into account that if you decide to use the Tree Model, acording to the official docs, the memory usage is proportional to content mapped (similar to data binding), so tree models can NOT be used with huge Json content, unless mapping is done chunk at a time. This is the same problem that data binding encounters; and sometimes the solution is to use Stream-of-Events instead.

只是评论。如您所知,Jackson支持3种主要处理模式(数据绑定,流API和树模型)。您需要考虑到,如果您决定使用树模型,根据官方文档,内存使用量与映射的内容成比例(类似于数据绑定),因此树模型不能与巨大的Json内容一起使用,除非映射一次完成。这与数据绑定遇到的问题相同;有时候解决方案就是使用Stream-of-Events。