{"id":1176,"date":"2012-12-19T14:39:22","date_gmt":"2012-12-19T14:39:22","guid":{"rendered":"http:\/\/brain.payzip.co.kr\/?p=1176"},"modified":"2012-12-19T14:39:22","modified_gmt":"2012-12-19T14:39:22","slug":"json-object-to-bean-java-beans-maps-arrays-and-collections","status":"publish","type":"post","link":"https:\/\/blog.goorigo.com\/?p=1176","title":{"rendered":"JSON Object to Bean (java beans, maps, arrays and collections)"},"content":{"rendered":"<h1><\/h1>\n<p>JSON Object to Bean<\/p>\n<div id=\"___plusone_0\"><a title=\"http:\/\/sourceforge.net\/projects\/json-lib\" href=\"http:\/\/sourceforge.net\/projects\/json-lib\" target=\"_blank\">http:\/\/sourceforge.net\/projects\/json-lib<\/a><\/div>\n<p><strong>JSON-lib<\/strong>\u00a0is a java library for serializing and de-serializing java beans, maps, arrays and collections in JSON format. Get the latest binaries from sourceforgehttp:\/\/json-lib.sourceforge.net\/. This requires the libraries (\u00a0json-lib-2.4-jdk15.jar,\u00a0xom-1.2.7.jar, ezmorph.jar commons-lang.jar, commons-collections.jar, commons-beanutils-1.7.jar and commons-logging-1.1.1.jar) to be in classpath. The following example shows converting a JSON object to Bean.<\/p>\n<p>160kB \uc800\uc6a9\ub7c9\uc5d0 \ubcf4\ub294 \uc774\ub4dd\uc774 \ub9ce\uc740\ub4ef<\/p>\n<table id=\"files_list\" title=\"This is a list of the files for this project\">\n<tbody>\n<tr title=\"json-lib-2.4-jdk15.jar\">\n<th headers=\"files_name_h\" scope=\"row\"><a title=\"Click to download json-lib-2.4-jdk15.jar\" href=\"http:\/\/sourceforge.net\/projects\/json-lib\/files\/json-lib\/json-lib-2.4\/json-lib-2.4-jdk15.jar\/download\">json-lib-2.4-jdk15.jar<\/a><\/th>\n<td headers=\"files_date_h\"><abbr title=\"2010-12-14 11:16:16 UTC\">2010-12-14<\/abbr><\/td>\n<td headers=\"files_size_h\">159.1 kB<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>[java]<br \/>\npackage com.bethecoder.tutorials.json_lib.common;import java.util.Date;<br \/>\npublic class Student {<br \/>\n  private String firstName;<br \/>\n  private String lastName;<br \/>\n  private int age;<br \/>\n  private String hobby;<br \/>\n  private Date dob;<\/p>\n<p>  public Student() {<br \/>\n  }<\/p>\n<p>  public Student(String firstName, String lastName, int age, String hobby,<br \/>\n      Date dob) {<br \/>\n    super();<br \/>\n    this.firstName = firstName;<br \/>\n    this.lastName = lastName;<br \/>\n    this.age = age;<br \/>\n    this.hobby = hobby;<br \/>\n    this.dob = dob;<br \/>\n  }<br \/>\n  public String getFirstName() {<br \/>\n    return firstName;<br \/>\n  }<br \/>\n  public void setFirstName(String firstName) {<br \/>\n    this.firstName = firstName;<br \/>\n  }<br \/>\n  public String getLastName() {<br \/>\n    return lastName;<br \/>\n  }<br \/>\n  public void setLastName(String lastName) {<br \/>\n    this.lastName = lastName;<br \/>\n  }<br \/>\n  public int getAge() {<br \/>\n    return age;<br \/>\n  }<br \/>\n  public void setAge(int age) {<br \/>\n    this.age = age;<br \/>\n  }<br \/>\n  public String getHobby() {<br \/>\n    return hobby;<br \/>\n  }<\/p>\n<p>  public void setHobby(String hobby) {<br \/>\n    this.hobby = hobby;<br \/>\n  }<br \/>\n  public Date getDob() {<br \/>\n    return dob;<br \/>\n  }<br \/>\n  public void setDob(Date dob) {<br \/>\n    this.dob = dob;<br \/>\n  }<\/p>\n<p>  public String toString() {<br \/>\n    return &quot;Student[ &quot; +<br \/>\n      &quot;firstName = &quot; + firstName +<br \/>\n      &quot;, lastName = &quot; + lastName +<br \/>\n      &quot;, age = &quot; + age +<br \/>\n      &quot;, hobby = &quot; + hobby +<br \/>\n      &quot;, dob = &quot; + dob +<br \/>\n      &quot; ]&quot;;<br \/>\n  }<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p>[java]<br \/>\npackage com.bethecoder.tutorials.json_lib.tests;import java.util.Date;<br \/>\nimport net.sf.json.JSONObject;<br \/>\nimport com.bethecoder.tutorials.json_lib.common.Student;<br \/>\npublic class Json2Bean {<\/p>\n<p>  \/**<br \/>\n   * @param args<br \/>\n   *\/<br \/>\n  public static void main(String[] args) {<br \/>\n    Student stud = new Student(&quot;Sriram&quot;, &quot;Kasireddi&quot;, 2, &quot;Singing&quot;, new Date(110, 4, 6));<br \/>\n    JSONObject obj = JSONObject.fromObject(stud);      \/\/BEAN to JSON<br \/>\n    System.out.println(obj.toString(2)); \/\/pretty print with indent<\/p>\n<p>    stud = (Student) JSONObject.toBean(obj, Student.class);  \/\/JSON to BEAN<br \/>\n    System.out.println(stud.toString());<br \/>\n  }<\/p>\n<p>}<\/p>\n<p>[\/java]<\/p>\n<p>It gives the following output,<\/p>\n<p>[java]<br \/>\n{<br \/>\n  &quot;age&quot;: 2,<br \/>\n  &quot;dob&quot;:   {<br \/>\n    &quot;date&quot;: 6,<br \/>\n    &quot;day&quot;: 4,<br \/>\n    &quot;hours&quot;: 0,<br \/>\n    &quot;minutes&quot;: 0,<br \/>\n    &quot;month&quot;: 4,<br \/>\n    &quot;seconds&quot;: 0,<br \/>\n    &quot;time&quot;: 1273084200000,<br \/>\n    &quot;timezoneOffset&quot;: -330,<br \/>\n    &quot;year&quot;: 110<br \/>\n  },<br \/>\n  &quot;firstName&quot;: &quot;Sriram&quot;,<br \/>\n  &quot;hobby&quot;: &quot;Singing&quot;,<br \/>\n  &quot;lastName&quot;: &quot;Kasireddi&quot;<br \/>\n}<\/p>\n<p>Student[ firstName = Sriram, lastName = Kasireddi,<br \/>\n\tage = 2, hobby = Singing, dob = Thu May 06 00:00:00 IST 2010 ]<\/p>\n<p>[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON Object to Bean http:\/\/sourceforge.net\/projects\/json-lib JSON-lib\u00a0is a java library for serializing and de-serializing java beans, maps, arrays and collections in JSON format. Get the latest binaries from sourceforgehttp:\/\/json-lib.sourceforge.net\/. This requires&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/posts\/1176"}],"collection":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1176"}],"version-history":[{"count":0,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/posts\/1176\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}