问题
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