Java uses Json-lib to manipulate JSON

Source: Internet
Author: User

1.1. Download Json-lib.jar

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

1.2. Java objects are converted to json1.2.1. The map object is converted to JSON

Map2json.java

Package json;

import Java.util.HashMap;

import Java.util.Map;

import Net.sf.json.JSONArray;

Public class Map2json {

Public static void main (string[] args) {

map<string, string> map = new hashmap<string, string> ();

Map.put (" name"," Zhang San");

Map.put (" Age","a");

Map.put (" gender"," male");

Jsonarray Jsonarray = Jsonarray. Fromobject (map);

System. out. println (Jsonarray.tostring ());

}

}

Operation Result:

[{"Gender": "Male", "name": "Zhang San", "Age": "22"}]

1.2.2. List conversion to JSON object

List2json.java

Package JSON;

Import java.util.ArrayList;

Import java.util.List;

Import Net.sf.json.JSONArray;

public class List2json {

public static void Main (string[] args) {

list<string> list = new arraylist<string> ();

List.add ("AA");

List.add ("BB");

List.add ("CC");

List.add ("DD");

Jsonarray Jsonarray = jsonarray.fromobject (list);

System.out.println (Jsonarray.tostring ());

}

}

Results:

["AA", "BB", "CC", "DD"]

1.2.3. Java Bean to JSON object

Person.java

Package json;

import java.util.Date;

Public class person {

Private Stringname;

Private Stringsex;

private int age;

Private Stringaddress;

Private Datebirthday;

Public String GetName () {

return name;

}

Public void setName (String name) {

this. name = name;

}

Public String Getsex () {

return sex;

}

Public void setsex (String sex) {

this. sex = sex;

}

Public int getage () {

return age;

}

Public void setage (int. ) {

this. age = age;

}

Public String getaddress () {

return address;

}

Public void setaddress (String address) {

this. address = address;

}

Public Date Getbirthday () {

return birthday;

}

Public void setbirthday (Date birthday) {

this. Birthday = birthday;

}

Public Person (string name, string sex,int age, string address,

Date birthday) {

Super ();

this. name = name;

this. sex = sex;

this. age = age;

this. address = address;

this. Birthday = birthday;

}

Public Person () {

Super ();

// TODO auto-generated Constructor stub

}

}

Bean2json.java

Package JSON;

Import Java.util.Date;

Import Net.sf.json.JSONArray;

public class Bean2json {

public static void Main (string[] args) {

person person = new person ();

person.setaddress (" Shenzhen Futian");

Person.setage (22);

Person.setbirthday (New Date ());

person.setname (" Zhang San");

person.setsex (" male");

Jsonarray Jsonarray = jsonarray.fromobject (person);

System.out.println (Jsonarray.tostring ());

}

}

Operation Result:

[{"Address": "Shenzhen Futian", "Age": "Birthday": {"date": +, "Day": 2, "hours": $, "minutes": "," "Month": 0, "Seconds": "Time ": 1422370042957," Timezoneoffset ": -480," Year ": [+]," name ":" Zhang San "," Sex ":" Male "}]

1.3. JSON conversion to map object

Json2map.java

Package json;

import Java.util.HashMap;

import Java.util.Map;

import java.util.Map.Entry;

import Java.util.Set;

import Net.sf.json.JSONObject;

Public class Json2map {

Public static void main (string[] args) {

String Jsonstr = "{' Gender ': ' male ', ' name ': ' Zhang San ', ' age ': ' 22 '}";

Jsonobject object = Jsonobject. Fromobject (JSONSTR);

map<string,string> map = new hashmap<string,string> ();

set<string> KeySet = object.keyset ();

for (String Key:keyset) {

Map.put (Key, Object.getstring (key));

}

set<entry<string,string>> EntrySet = Map.entryset ();

for (entry<string, string> entry:entryset) {

System. out. println (Entry.getkey () +":"+entry.getvalue ());

}

}

}

Operation Result:

Gender: Male

Name: Zhang San

Age: 22

Java uses Json-lib to manipulate JSON

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.