Parse Jackson JSON array of integers to Java array without using extra classes

我们两清 提交于 2021-01-07 07:50:20

问题


I have an array of integers

[2, 41, 52, 54, 23, 65, 4]

How to parse these values into a simple Java integer array from that JSON array without using extra classes? Is that possible?


回答1:


Sure. Just like you would do with any other object:

public class ParseArray {
    public static void main(String[] args) throws IOException {
        String json = "[1, 2, 3]";
        int[] array = new ObjectMapper().readValue(json, int[].class);
        System.out.println("array = " + Arrays.toString(array));
    }
}



回答2:


This example could help you:

JSON : {"integers":[2, 41, 52, 54, 23, 65, 4]}

JSONArray array = obj.optJSONArray("integers");

Then:

int[] integers = new int[array.length()];



来源:https://stackoverflow.com/questions/34698980/parse-jackson-json-array-of-integers-to-java-array-without-using-extra-classes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!