Java constructs and parses JSON data in one of two ways

Source: Internet
Author: User

First, Introduction

The Json-lib package is a Beans,collections,maps,java arrays and XML and JSON to convert the package, is mainly used to parse JSON data, on its official website http://www.json.org/detailed explanation, Interested to be able to study.

Download jar Dependency Package: You can go here to download

Iii. Introduction to the basic methods

1. Convert list collection to JSON method

New ArrayList (); List.add ("First" ); List.add ("second" ); Jsonarray jsonArray2 = jsonarray.fromobject (list);  

2. Map collection into JSON method

New HashMap (); Map.put ("Name", "JSON"), Map.put ("bool"new Integer (1new string[] {"A", "B" }); Map.put ("func", "function (i) {return this.arr[i];}" ); Jsonobject JSON = jsonobject.fromobject (map);     

3. The bean is converted into JSON code

Jsonobject jsonobject = Jsonobject.fromobject (new Jsonbean ());

4. Convert arrays into JSON code

True }; Jsonarray jsonArray1 = Jsonarray.fromobject (Boolarray);

5. Conversion of general data into JSON code

Jsonarray jsonArray3 = Jsonarray.fromobject ("[' json ', ' is ', ' easy ']");

6. Beans conversion to JSON code

Newnew JsonBean2 (); Jb1.setcol (1); Jb1.setrow (1); Jb1.setvalue ("xx"new JsonBean2 (); Jb2.setcol (2); Jb2.setrow (2); Jb2.setvalue (""); List.add (JB1); List.add (JB2); Jsonarray ja = jsonarray.fromobject (list);        

Iv. Demonstration Examples

Here are some basic common methods for testing

PackageCom.json;ImportJava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.List;ImportJava.util.Map;ImportNet.sf.json.JSONArray;ImportNet.sf.json.JSONObject;/*** Construct and parse JSON data using Json-lib * *@authorAlexia * @date 2013/5/23 **/PublicClassjsontest {/*** Construct JSON Data * *@return*/PublicStaticString Buildjson () {//JSON-formatted data parsing object Jsonobject Jo =NewJsonobject ();//The following constructs two maps, a list, and an Employee object map<string, string> map1 =New Hashmap<string, string>(); Map1.put ("name", "Alexia"); Map1.put ("Sex", "female"); Map1.put ("Age", "23"); map<string, string> map2 =New Hashmap<string, string>(); Map2.put ("name", "Edward"); Map2.put ("Sex", "male"); Map2.put ("Age", "24"); list<map> list =New arraylist<map>(); List.add (MAP1); List.add (MAP2); Employee Employee =NewEmployee (); Employee.setname ("Wjl"); Employee.setsex ("female"); Employee.setage (24);//Convert map to Jsonarray data Jsonarray ja1 =Jsonarray.fromobject (MAP1);//Convert list to Jsonarray data Jsonarray JA2 =Jsonarray.fromobject (list);//Convert bean to Jsonarray data jsonarray ja3 =Jsonarray.fromobject (employee); System.out.println ("Jsonarray Object Data format:"); System.out.println (Ja1.tostring ()); System.out.println (Ja2.tostring ()); System.out.println (Ja3.tostring ());//Constructs JSON data, including a map and an employee object Jo.put ("map", JA1); Jo.put ("Employee", JA2); System.out.println ("\ n final constructed JSON data format:"); System.out.println (Jo.tostring ());ReturnJo.tostring (); }/*** Parse JSON data * *@paramJsonstring JSON Data string*/PublicStaticvoidParsejson (String jsonstring) {//Take employee as an example, map similar to jsonobject JB =Jsonobject.fromobject (jsonstring); Jsonarray ja = Jb.getjsonarray ("Employee"); List<employee> emplist =New arraylist<employee>();//Loop to add an employee object (there may be multiple)for (int i = 0; I < ja.size (); i++) {Employee Employee =NewEmployee (); Employee.setname (Ja.getjsonobject (i). getString ("name")); Employee.setsex (Ja.getjsonobject (i). getString ("Sex")); Employee.setage (Ja.getjsonobject (i). GetInt ("Age")); Emplist.add (employee); } System.out.println ("\ n convert JSON data to employee object:"); for (int i = 0; i < emplist.size (); I++) {Employee emp = Emplist.get (i); System.out.println ("Name:" + emp.getname () + "Sex:" + emp.getsex () + "Age:" + Emp.getag E ()); }} /**  @param  args */public static Span style= "color: #0000ff;" >void Main (string[] args) {//  TODO auto-generated method Stub Parsejson (Buildjson ()) ; }} 

The operation results are as follows

Ext.: http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/23/3096001.html

Java constructs and parses JSON data in one of two ways

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.