Flexjsonest une bibliothèquelégèrepermettant de sérialiser et de désérialiser des objets Java vers et depuis le format JSON. Nous pouvons désérialiser les objets Java d'un flux Reader en utilisant la méthodedeserialize()de la classeJSONDeserializer, qui utilise une instance de la classeReadercomme entrée JSON.
public T deserialize(Reader input)
import java.io.*; import flexjson.JSONDeserializer; public class JSONDeserializeReaderTest { public static void main(String[] args) { JSONDeserializerdeserializer = new JSONDeserializer (); String jsonStr = "{" + "\"firstName\": \"Adithya\"," + "\"lastName\": \"Sai\"," + "\"age\": 25," + "\"address\": \"Hyderabad\"" + "\"class\": \"Student\"" + "}"; Student student = deserializer.deserialize(new StringReader(jsonStr)); System.out.println(student); } } // Student class class Student { private String firstName; private String lastName; private int age; private String address; public Student() {} public Student(String firstName, String lastName, int age, String address) { super(); this.firstName = firstName; this.lastName = lastName; this.age = age; this.address = address; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String toString() { return "Student[ " + "firstName = " + firstName + ", lastName = " + lastName + ", age = " + age + ", address = " + address + " ]"; } }
Student[ firstName = Adithya, lastName = Sai, age = 25, address = Hyderabad ]
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!