Java object and json data conversion method 1-use json-lib

Source: Internet
Author: User

Java object and json data conversion method 1-use json-lib

Test code:


Package com. yanek. util. json; import java. util. arrayList; import java. util. list; import net. sf. json. JSONArray; import net. sf. json. JSONObject; public class JSONLibUtil {/*** @ param args */public static void main (String [] args) {Hotel h1 = new Hotel (); h1.setId (1 ); h1.setName ("name1"); Hotel h2 = new Hotel (); h2.setId (2); h2.setName ("name2"); RoomTypeVO t1 = new RoomTypeVO (); t1.setPrice ("20"); t1.setTypeid (1); t1.setTypename ("nnn1"); List
 
  
RoomTypeVOs = new ArrayList
  
   
(); RoomTypeVOs. add (t1); RoomTypeVOs. add (t1); h1.setRoomTypeVOs (RoomTypeVOs); Hotel hotel = new Hotel (); System. out. println ("json1:" + bean2Json (h1); String s = bean2Json (h1); hotel = json2Bean (s, Hotel. class); System. out. println ("json2:" + bean2Json (hotel); System. out. println ("json3:" + beanlist2Json (RoomTypeVOs); String s2 = beanlist2Json (RoomTypeVOs); Object [] list = getDTOArray (s2, RoomTypeVO. class); for (Object v: list) {RoomTypeVO rt = (RoomTypeVO) v; System. out. println (rt. getTypeid () + "-" + rt. getTypename () + "-" + rt. getPrice () ;}} public static String bean2Json (Object obj) {JSONObject jsonObject = JSONObject. fromObject (obj); return jsonObject. toString ();} public static String beanlist2Json (Object obj) {JSONArray jsonObject = JSONArray. fromObject (obj); return jsonObject. toString () ;}@ SuppressWarnings ("unchecked") public static
   
    
T json2Bean (String jsonStr, Class
    
     
ObjClass) {return (T) JSONObject. toBean (JSONObject. fromObject (jsonStr), objClass) ;}@ SuppressWarnings ("unchecked") public static
     
      
T jsonarray2Bean (String jsonStr, Class
      
        ObjClass) {return (T) JSONObject. toBean (JSONObject. fromObject (jsonStr), objClass);}/*** get a java object array from a json array, for example, * [{"id": idValue, "name ": nameValue}, {"id": idValue, "name": nameValue},...] * @ param object * @ param clazz * @ return */public static Object [] getDTOArray (String jsonString, Class clazz) {JSONArray array = JSONArray. fromObject (jsonString); Object [] obj = new Object [array. size ()]; for (int I = 0; I <array. size (); I ++) {JSONObject jsonObject = array. getJSONObject (I); obj [I] = JSONObject. toBean (jsonObject, clazz);} return obj ;}}
      
     
    
   
  
 

The output is as follows:

json1:::{"id":1,"name":"name1","roomTypeVOs":[{"price":"20","typeid":1,"typename":"nnn1"},{"price":"20","typeid":1,"typename":"nnn1"}]}json2:::{"id":1,"name":"name1","roomTypeVOs":[{"price":"20","typeid":1,"typename":"nnn1"},{"price":"20","typeid":1,"typename":"nnn1"}]}json3:::[{"price":"20","typeid":1,"typename":"nnn1"},{"price":"20","typeid":1,"typename":"nnn1"}]1-nnn1-201-nnn1-20

Related java entity classes:

package com.yanek.util.json;public class RoomTypeVO {public int getTypeid() {return typeid;}public void setTypeid(int typeid) {this.typeid = typeid;}public String getTypename() {return typename;}public void setTypename(String typename) {this.typename = typename;}public String getPrice() {return price;}public void setPrice(String price) {this.price = price;}private int typeid;private String typename;private String price;}package com.yanek.util.json;import java.util.List;public class Hotel {public List
 
   getRoomTypeVOs() {return RoomTypeVOs;}public void setRoomTypeVOs(List
  
    roomTypeVOs) {RoomTypeVOs = roomTypeVOs;}private List
   
     RoomTypeVOs;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}private int id;private String name;}
   
  
 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.