将JSON数组解析为Java对象

将JSON数组解析为Java对象

本文介绍了将JSON数组解析为Java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jackson映射器进行解析,以将大JSON解析为java对象.我有一个很大的JSON,但遇到了其中的小片段,不确定如何解析.
这是JSON,其格式看起来几乎没有什么不同.我试图了解如何将其解析为对象.

I am trying to parse using Jackson mapper to parse big JSON to java object. I have very big JSON but came across this little piece in it and not sure how to parse.
Here is the JSON, the format of it looks little different. I am trying to understand how can I parse it to an object.

{
    "coordinates": [
        [
            [
                -72.943068,
                45.842298
            ],
            [
                -72.943075,
                45.841859
            ]
        ]
    ]
}

我不知道它采用的是哪种格式,以及如何将其解析为对象.

I don't understand which format it is in, and how I can parse it to object.

推荐答案

这取决于您的JSON有多大.如果可以将其加载到内存中,则可以使用最简单的方法:解决方案1:
POJO类:

It depends how really big is your JSON. If you can load it to memory, you can use the simplest way:Solution 1:
POJO class:

class CoordinatesContainer {

    private double[][][] coordinates;

    public double[][][] getCoordinates() {
        return coordinates;
    }

    public void setCoordinates(double[][][] coordinates) {
        this.coordinates = coordinates;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder(1024);
        for (double[] arrayItem : coordinates[0]) {
            builder.append(Arrays.toString(arrayItem));
            builder.append(", ");
        }
        return builder.toString();
    }
}

用法:

ObjectMapper mapper = new ObjectMapper();
CoordinatesContainer coordinatesContainer = mapper.readValue(json, CoordinatesContainer.class);
System.out.println(coordinatesContainer);

程序上方打印:

[-72.943068, 45.842298], [-72.943075, 45.841859]

解决方案2:
但是,如果您的JSON确实很大并且无法将其加载到内存中,则应考虑杰克逊流API .在这种情况下,您不应该创建POJO类并尝试按节点"来处理每个元素节点":

Solution 2:
But if your JSON is really big and you are not able to load it to memory, you should consider Jackson Streaming API. In this case you should not create POJO class and try to process each element "node" by "node":

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;

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

public class JsonProgram {

    public static void main(String[] args) throws IOException {
        File json = new File("/x/data.json");
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser parser = jsonFactory.createParser(json);
        // Skip all elements to first array
        while (parser.nextToken() != JsonToken.START_ARRAY) {
        }
        parser.nextToken();
        // First level
        while (parser.nextToken() != JsonToken.END_ARRAY) {
            // Skip inner start array element
            parser.nextToken();
            System.out.println();
            System.out.println("NEXT ARRAY NODE");

            BigDecimal first = parser.getDecimalValue();

            // Go to second value
            parser.nextToken();

            BigDecimal second = parser.getDecimalValue();

            // Skip inner end array element
            parser.nextToken();

            // Handle array item
            System.out.println("First: " + first.toString());
            System.out.println("Second: " + second.toString());
        }
    }
}

程序上方打印:

NEXT ARRAY NODE
First: -72.943068
Second: 45.842298

NEXT ARRAY NODE
First: -72.943075
Second: 45.841859

在我的示例中,我使用了2.2.3版本的Jackson.

In my examples I used Jackson in 2.2.3 version.

这篇关于将JSON数组解析为Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:41