Json-lib using--jsonobject and Jsonarray

Source: Internet
Author: User

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);
4
5 Book book2=new Book ();
6 book2.setname ("C");
7 Book2.setprice (42.3f);
8 List list=new ArrayList ();
9 list.add (book);
Ten List.add (BOOK2);
jsonarray arry=jsonarray.fromobject (list);
System.out.println (arry.tostring ());
13//results are as follows:
[{' 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);
3 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 ");
4?

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);
4 for (Product p:products) {
5 System.out.println (p.getname () + "" +p.getprice ());
6 }

Customizing classes that encapsulate JSON operations

1 package com.util;
2
3 Import java.util.List;
4 Import Java.util.Map;
5
6 Import Net.sf.json.JSONArray;
7 Import Net.sf.json.JSONObject;
8
9 public class Jsonhelper {
10//Convert from normal bean to string
public static String Getjson (Object o) {
Jsonobject Jo=jsonobject.fromobject (o);
return jo.tostring ();
14}
15//Convert from List of Java to string
public static String Getjson (List list) {
Jsonarray ja=jsonarray.fromobject (list);
return ja.tostring ();
19}
20//Convert from a Java object array to a string
public static String Getjson (object[] arry) {
Jsonarray Ja=jsonarray.fromobject (Arry);
Ja.tostring return ();
24}
25//Convert from JSON-formatted string to map object
public static Map GetObject (String s) {
return Jsonobject.fromobject (s);
28}
29//Convert from JSON-formatted string to list array
public static List GetArray (String s) {
return Jsonarray.fromobject (s);
32}
33//Convert from JSON-formatted string to a bean
public static Object GetObject (String s,class cls) {
Jsonobject Jo=jsonobject.fromobject (s);
Jsonobject.tobean return (Jo, CLS);
37}
38//Convert from JSON-formatted string to an array of objects of a class
GetArray public static Object (String S,class cls) {
Jsonarray Ja=jsonarray.fromobject (s);
Jsonarray.toarray return (JA, CLS);
42}
43}

Json-lib using--jsonobject and Jsonarray

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.