JSON parsing example-using Json-lib

Source: Internet
Author: User

json-lib Download and use

This article describes using a class library for JSON parsing.

Tool: Http://sourceforge.net/projects/json-lib/?source=directory  

After downloading, there are so three things:

Importing the first jar package into a project is the API in which it is available.

(How to: Create a Libs folder in a project, copy the first package in, and then add External JARs in the Java Build path ... )

  The second package is a document, directly after the right-click to extract the HTML document, the portal is index.html, of course, you can also use the tool (Jd2chm.exe) to convert it to the CHM format to see more convenient.   

The third package is the source code, interested in or later encountered problems can be seen.

Add-on package: Resolve Exception in thread "main" Java.lang.NoClassDefFoundError  

After importing the above package, the runtime throws a run-time exception:Exception in thread "main" Java.lang.NoClassDefFoundError.

  after searching, I didn't know to add another package, which I downloaded from here:  

http://download.csdn.net/download/guoyongxue/4423006

  If you do not have points, you can also go to my web drive to download:

  http://pan.baidu.com/share/link?shareid=1066468750&uk=2701745266

In other words, it takes so many packages:

You'll need to add them all to the Libs folder, adding them into the build path.

Package Com.jsonlib.test;public class Person {private int id;private String name;public person () {}public person (int ID, S Tring name) {this.id = Id;this.name = name;} 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;} @Overridepublic String toString () {return "person [id=" + ID + ", name=" + name + "]";}}
Test class

Package Com.jsonlib.test;import Java.util.arraylist;import Java.util.hashmap;import java.util.list;import  Java.util.map;import Net.sf.json.jsonobject;public class Jsondataconvert_test {/** * Create JSON String * @param key * @param value * @return */public static string createjsonstring (string key, Object value) {jsonobject jsonobject = new Jsonobject (); JSO Nobject.put (key, value); return jsonobject.tostring ();} public static void Init () {///A User object converts person person = new Man (001, "BBW"); System.out.println (createjsonstring ("person"));//User Object collection conversion list<person> personlist = new arraylist< Person> (); Person Person1 = new Person (001, "Liu Renqui"); Person Person2 = new Person (002, "Cow month");p Ersonlist.add (Person1);p ersonlist.add (Person2); System.out.println (createjsonstring ("Personlist", personlist));//String Collection conversion list<string> stringlist = new Arraylist<string> (); Stringlist.add ("X-rapido"); Stringlist.add ("Niuyue");//list in Map set conversion list<map< String, string>> maplist = new arraylist&Lt Map<string, string>> (); for (int i = 0; i < 3; ++i) {map<string, string> Map = new hashmap<string, Str Ing> (); Map.put ("Number", "id_" + i) map.put ("Address", "name_" + i); Maplist.add (map);} System.out.println (createjsonstring ("Maplist", Maplist));} public static void Main (string[] args) {init ();}}
Print output:

{"Person": {"id": 1, "name": "BBW"}} {"Personlist": [{"id": 1, "name": "Liu Renqui"},{"id": 2, "name": "Cow Month"}]} {"Maplist": [{"Number": "Id_0", "Address": "Name_0"},{"number": "Id_1", "Address": "Name_1"},{"number": "Id_2", "Address": "Name_2"}]}


References

  Json-lib Tool Class Library:

http://sourceforge.net/projects/json-lib/?source=directory

http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-2.4/

  Additional Packages:

http://download.csdn.net/download/guoyongxue/4423006

  Baidu Network disk in all json-lib resources download:

http://pan.baidu.com/share/link?shareid=1066468750&uk=2701745266


JSON parsing instance-using Json-lib

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.