Boon - From Object


Advertisements

ObjectMapper class can be used to generate a json string from an Object.

Example

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

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

public class BoonTester {
   public static void main(String args[]){
      ObjectMapper mapper = JsonFactory.create();      
      Student student = new Student("Mahesh", 21);
      String jsonString = mapper.writeValueAsString(student);
      System.out.println(jsonString);
   }
}
class Student {
   public String name;
   public int age;
   public Student(String name, int age) {
      this.name = name;
      this.age = age;
   }
}

Output

This produces the following output −

{"name":"Mahesh","age":21}
Advertisements