Java: Convert JSON String Array to Objects
You can use a JSON library to convert a JSON string array into Java objects.
For example, using the Jackson library:
import com.fasterxml.jackson.databind.ObjectMapper;
String json = '[{"name":"John","age":30},{"name":"Jane","age":25}]';
ObjectMapper mapper = new ObjectMapper();
Person[] persons = mapper.readValue(json, Person[].class);
Here, Person is a custom Java class used to represent the objects in the JSON:
public class Person {
private String name;
private int age;
// getters and setters
}
In this example, the readValue method converts the JSON string array into an array of Person objects.
原文地址: https://www.cveoy.top/t/topic/mUhE 著作权归作者所有。请勿转载和采集!