For the use of the json-lib class library, the json-lib class library

Source: Internet
Author: User

For the use of the json-lib class library, the json-lib class library

Preface:

The json-lib package is a Java class library that converts Java objects (including beans, maps, collections, java arrays, and xml) to JSON.

Similarly, Google also launched a class library named Gson, which will not be discussed today.

 

Preparations:

First, we need to download the jar package of json-lib and import the project

 

Because the method is simple, the code is displayed here.

 

1. First, write a json tool class and input 2 parameters. 1 is the json identifier (custom), and 2 is the object to be converted into a json string format.

1 package com. lcw. json. util; 2 3 import net. sf. json. JSONObject; 4 5 public class MakeJson {6 7/** 8*9 * @ param key json identifier 10 * @ param value json content (multiple types, class types, strings, list set, etc.) 11 * @ return returns a json expression 12 */13 public static String getJson (String key, Object value) {14 JSONObject jsonObject = new JSONObject (); 15 jsonObject. put (key, value); // assign 16 String info = jsonobject to the jsonObject object. toString (); 17 return info; 18} 19 20}

 

2. Provide a data source class

1 package com. lcw. json. service; 2 3 import java. util. arrayList; 4 import java. util. hashMap; 5 import java. util. list; 6 import java. util. map; 7 8 import com. lcw. json. vo. person; 9 10 public class JsonService {11 12 // get a Person object 13 public Person getPerson () {14 Person person Person = new Person (1, "tuzi", 22 ); // instantiate a Person object 15 return person; 16} 17 18 // get a List set (storing the Person type) 19 public List <Person> getListPerson () {20 List <Person> list = new ArrayList <Person> (); 21 Person person1 = new Person (1, "lcw", 20 ); 22 Person person2 = new Person (2, "tuzi", 22); 23 list. add (person1); 24 list. add (person2); 25 return list; 26 27} 28 // get a List set (String type) 29 public List <String> getInfo () {30 List <String> list = new ArrayList <String> (); 31 list. add ("Beijing"); 32 list. add ("Shanghai"); 33 list. add ("Guangzhou"); 34 return list; 35} 36 37 38 // get a List set (storing the Map type) 39 public List <Map <String, object> getListPersons () {40 List <Map <String, Object> list = new ArrayList <Map <String, Object> (); 41 42 Map <String, object> map1 = new HashMap <String, Object> (); 43 Person person1 = new Person (1, "lcw", 20); 44 map1.put ("person1", person1 ); 45 46 Map <String, Object> map2 = new HashMap <String, Object> (); 47 Person person2 = new Person (2, "tuzi", 22 ); 48 map2.put ("person2", person2); 49 50 list. add (map1); 51 list. add (map2); 52 53 return list; 54 55} 56 57 58}

 

3. entity class

 1 package com.lcw.json.vo; 2  3 public class Person { 4  5     private int id; 6     private String name; 7     private int age; 8  9     public Person(int id, String name, int age) {10         super();11         this.id = id;12         this.name = name;13         this.age = age;14     }15 16     public int getId() {17         return id;18     }19 20     public void setId(int id) {21         this.id = id;22     }23 24     public String getname() {25         return name;26     }27 28     public void setname(String name) {29         this.name = name;30     }31 32     public int getAge() {33         return age;34     }35 36     public void setAge(int age) {37         this.age = age;38     }39 40     @Override41     public String toString() {42         return "Person [age=" + age + ", id=" + id + ", name=" + name + "]";43     }44 45 }

 

4. Testing

 1 package com.lcw.json.test; 2  3 import java.util.List; 4 import java.util.Map; 5  6 import org.junit.Test; 7  8 import com.lcw.json.service.JsonService; 9 import com.lcw.json.util.MakeJson;10 import com.lcw.json.vo.Person;11 12 public class JsonTest {13 14     @Test15     public void getPersonJson() {16         JsonService jsonService = new JsonService();17         Person p1 = jsonService.getPerson();18         String info = MakeJson.getJson("person", p1);19         System.out.println(info);20     }21 22     @Test23     public void getListPersonJson() {24         JsonService jsonService = new JsonService();25         List<Person> persons = jsonService.getListPerson();26         String info = MakeJson.getJson("persons", persons);27         System.out.println(info);28 29     }30 31     @Test32     public void getListString() {33         JsonService jsonService = new JsonService();34         List<String> news = jsonService.getInfo();35         String info = MakeJson.getJson("info", news);36         System.out.println(info);37     }38     39     @Test40     public void getListPersonsJson() {41         JsonService jsonService = new JsonService();42         List<Map<String,Object>> persons = jsonService.getListPersons();43         String info = MakeJson.getJson("persons", persons);44         System.out.println(info);45 46     }47     48 49 }

 

The above are four unit test classes. The code is very simple, so no text explanation will be made. Let's see how to run it:

 

 

 

 

If you want to make the server for remote calls, print the data to the webpage. Pai_^


Json-lib usage Problems

This class name. class
I want to convert a json string into a list set of object classes.
This is the entity class name. class.
Json-lib.sourceforge.net/usage.html
This is an example of an official website.

, Json-lib parses json data

Public class GetJson {public static void main (String [] args) {// The error code returned by the main method. StaticString json = "{'name': 'Baby-friendly ', 'array': [{'A': '000000',' B ': '000000 ', 'C': '000000'}, {}, {'A': '000000'}], 'address': 'Baby-friendly '} "; try {JSONObject jsonObject = JSONObject. fromObject (json); String name = jsonObject. getString ("name"); String address = jsonObject. getString ("address"); System. out. println ("name is:" + name); System. out. println ("address is:" + address); JSONArray jsonArray = jsonObject. getJSONArray ("array"); for (int I = 0; I <jsonArray. size (); I ++) {System. out. println ("item" + I + ":" + jsonArray. getString (I) ;}} catch (JSONException e) {e. printStackTrace () ;}} modified the main method.


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.