The Java object and the JSON string are transferred to each other

Source: Internet
Author: User

1:java object and JSON string conversion:

Java Object-json string:

Jsonobject jsonstr = Jsonobject.fromobject (object);

String str = jsonstr.tostring ();

JSON string-java object:

Jsonobject jsonobject = Jsonobject.fromobject (jsonstring);

Object Pojo = Jsonobject.tobean (JSONOBJECT,POJOCALSS);

2:java array objects with JSON string conversions:

Java Array-json string:

Jsonarray arraystr = jsonarray.fromobject (list<?>);

String str = arraystr.tostring ();

JSON string-java array:

Jsonarray array = jsonarray.fromobject (str);

list<?> list = jsonarray.tolist (array,?. Class);

1. from object to string
To construct a jsonobject or Jsonarray object with object, then call its ToString () method to

(1) Example one

1 book book=new Book ();  2 Book.setname ("Java"); 3 Book.setprice (52.3f); 4 jsonobject object=jsonobject.fromobject (book); 5 System.out.println (object.tostring ());    

(2) Example two

1 book book=new book ();2 Book.setname ("Java");3 Book.setprice (52.3f);45 Book Book2=new book ();6 Book2.setname ("C"); 7 Book2.setprice (42.3f);  8 List list=new ArrayList ();  9 List.add (book); 10 List.add (BOOK2); 11 jsonarray arry=jsonarray.fromobject (list); 12 System.out.println (Arry.tostring ()); 13 //14 [{"Name": "Java", "Price": 52.3},{"name": "C", "Price": 42.3}]    

2. From string to Object
To construct a jsonobject or Jsonarray object with a string object first
(1) Example one

1 String json= "{name: ' Java ', price:52.3}";     2 jsonobject object=jsonobject.fromobject (JSON);  3 System.out.println (Object.get ("name") + "" "+object.get (" price "); 

(2) Example two

1 String json= "[{name: ' Java ', price:52.3},{name: ' C ', price:42.3}]";  2 Jsonarray array=jsonarray.fromobject (JSON);  for (int i=0;i<array.size (); i++) {4 map o= (map) array.get (i);  5 System.out.println (O.get ("name") + "" "+o.get (" price "); 6}     

3. From string to Bean

(1) Single Bean object

1         String json= "{name: ' Java ', price:52.3}";  2         Jsonobject Object=jsonobject.fromobject (JSON); 3 Product product= (product) Jsonobject.tobean (object,product.  Class); 4 System.out.println (product.getname () + "" +product.getprice ());   

(2). An array of beans

1         String json= "[{name: ' Java ', price:52.3},{name: ' C ', price:42.3}]";  2         Jsonarray Array=jsonarray.fromobject (JSON); 3 product[] products= (product[]) Jsonarray.toarray (array,product.  Class); For (Product p:products) {5 System.out.println (p.getname () + "" +p.getprice ());  6}     

Customizing classes that encapsulate JSON operations

1Package com.util;23Import java.util.List;4Import Java.util.Map;56Import Net.sf.json.JSONArray;7Import Net.sf.json.JSONObject;89PublicClass Jsonhelper {10//Converting from a normal bean to a string11PublicStatic String Getjson (Object o) {Jsonobject Jo=jsonobject.fromobject (o);13return jo.tostring ();14}15//Converting from a list of Java to a string16PublicStatic String Getjson (List list) {Jsonarray ja=jsonarray.fromobject (list);18return ja.tostring ();19}20//Converting from a Java object array to a string21stPublicStatic String Getjson (object[] arry) {Jsonarray Ja=jsonarray.fromobject (Arry);23return ja.tostring ();24}25//Convert a string from JSON format to a map object26PublicStatic Map GetObject (String s) {27return Jsonobject.fromobject (s);28}29//Convert from JSON-formatted string to list array30PublicStatic List GetArray (String s) {31return Jsonarray.fromobject (s);32}33//Converting from a JSON-formatted string to a bean34PublicStatic Object GetObject (String s,class cls) {Jsonobject Jo=jsonobject.fromobject (s);36 return Jsonobject.tobean (Jo, CLS); 37}38 // convert from a JSON-formatted string to an array of objects of a class public static Object GetArray (String s,class cls) { Span style= "FONT-SIZE:16PX;" >40 Jsonarray Ja=jsonarray.fromobject (s); 41 return Jsonarray.toarray (JA, CLS); 42}43}                 

The Java object and the JSON string are transferred to each other

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.