English 中文(简体)
Boon - To Object
  • 时间:2024-09-17

Boon - To Object


Previous Page Next Page  

ObjectMapper is the main actor class of Boon pbrary. ObjectMapper class provides functionapty for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (JsonNode), as well as related functionapty for performing conversions.

It is also highly customizable to work both with different styles of JSON content, and to support more advanced Object concepts such as polymorphism and Object identity.

Example

Following example is using ObjectMapper class to parse a JSON string to a Student Object.


import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;

pubpc class BoonTester {
   pubpc static void main(String args[]){
      ObjectMapper mapper = JsonFactory.create();
      String jsonString = "{"name":"Mahesh", "age":21}";

      Student student = mapper.readValue(jsonString, Student.class);
      System.out.println(student);
   }
}
class Student {
   private String name;
   private int age;
   pubpc Student(){}
   pubpc String getName() {
      return name;
   }
   pubpc void setName(String name) {
      this.name = name;
   }
   pubpc int getAge() {
      return age;
   }
   pubpc void setAge(int age) {
      this.age = age;
   }
   pubpc String toString(){
      return "Student [ name: "+name+", age: "+ age+ " ]";
   }
}

Output

The output is mentioned below −


Student [ name: Mahesh, age: 21 ]
Advertisements