JSON basic usage

Source: Internet
Author: User

1 The data class (gets to be converted to JSON)

Package org.data;

Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;

public class Data {

Public list<string>getList (){
list<string> list = new arraylist<string> ();
List.add ("Xixi");
List.add ("haha");
List.add ("SB");
return list;
}
Public map<string, string>Getmap (){
map<string, string> map = new hashmap<string, string> ();
Map.put ("name", "Xiao Bo");
Map.put ("Sex", "male");
Map.put ("Age", "Infinity");
Map.put ("The Other Half", "no");
Map.put ("First Love", "ran");
return map;
}
Public boolean[]GetArray (){
boolean[] Boolarray = new boolean[] {true, false, true};
return boolarray;
}
}
2Bean (JavaBean can also be converted to JSON)Note: To add getter and setter

Package org.data;

public class Jsonbean {
private String name;
Private String sex;
Private String age;

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 String Getage () {
return age;
}
public void Setage (String age) {
This.age = age;
}
Public Jsonbean (String a,string B, string c) {
This.setname (a);
This.setsex (b);
This.setage (b);
}
}

3 Test class

Package org.data;

Import java.util.List;
Import Java.util.Map;

Import Net.sf.json.JSONArray;
Import Net.sf.json.JSONObject;

public class Parsejsontest {

public static void Main (string[] args) {
TODO auto-generated Method Stub
Data data=new data ();
List list=data.getlist ();
Map Map=data.getmap ();
Boolean[] B=data.getarray ();


Jsonarray jsonarrayfromlist = jsonarray.fromobject (list);
Jsonobject Jsonobjectfrommap = jsonobject.fromobject (map);
Jsonarray Jsonarrayfromarray = Jsonarray.fromobject (b);

System.out.println (jsonarrayfromlist);
System.out.println (JSONOBJECTFROMMAP);
System.out.println (Jsonarrayfromarray);


Jsonarray jsonarrayfromstring= jsonarray.fromobject ("[' json ', ' is ', ' easy ']");
System.out.println (jsonarrayfromstring);


Jsonobject Jsonobjectfrombean = jsonobject.fromobject (New Jsonbean ("Xiao Bo", "Male", "at"));
System.out.println (Jsonobjectfrombean);

}

}
4

5 Required jar Package ( download Json-lib folder )



JSON basic usage

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.