First, why use Jsonarray and Jsonobject
1, backstage-"The foreground can convert Java objects and collections into JSON string format, so in the foreground of the Ajax method can be directly converted to JSON object use, from the background to the foreground value
2, the foreground-"use Json.stringify (mdses) method to put JS object, array." Convert to a JSON string and use Jsonarray or jsonobject in the background to convert to Java objects or collections
Helps to pass the front-end parameters to the backend. And back-end value Y
3. JSON is now a carrier for most data transmission
Second, the background to resolve the front desk Ajax submission data, using less commonly used to convert a list or map to JSON form of string values to the foreground of Ajax:
Parses the data and data objects included in the string according to the received JSON string @testpublic void Parsjsonfromfront () {//received jsonstring result = "[{\" username\ ": \" Your name\ ", \" user_json\ ": {\" username\ ": \" Your name\ ", \" nickname\ ": \" Your Nickname\ "}}]"; Generates a JSON object based on string Chen//based on string//The assumption is that the form of an array is converted to a Jsonarray object using Jsonarray.fromobject (Result)//assumed to be a JSON object is to use Jsonobject.fromobject (result) to convert to Jsonobject object Jsonarray resultarray = Jsonarray.fromobject (result); Jsonobject jsonobject = resultarray.getjsonobject (0); Jsonobject User_json = Jsonobject.getjsonobject ("User_json");//value by key value string username = user_json.getstring (" Username "); String nickname = user_json.getstring ("nickname"); System.out.println ("username =" + username + ":" + "nickname =" + nickname);//console: username =your Name:nickn Ame = Your Nickname}
Third, the back-end conversion list or map into a JSON form of string value to the foreground of Ajax
User Bean
public class User {private String name;private Adress adress;private list<string> Students;public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Adress getadress () {return Adress;} public void setadress (Adress Adress) {this.adress = Adress;} Public list<string> getstudents () {return students;} public void Setstudents (list<string> students) {this.students = students;} Public User (String name, Adress Adress, list<string> students) {super (); this.name = Name;this.adress = Adress;this. Students = students;} Public User () {super ();}}
public class Adress {private string Streetname;private string District;public string Getstreetname () {return streetname;} public void Setstreetname (String streetname) {this.streetname = StreetName;} Public String getdistrict () {return district;} public void Setdistrict (String district) {this.district = district;} Public Adress (String streetname, String district) {super (); this.streetname = Streetname;this.district = District;} Public Adress () {super ();//TODO auto-generated constructor stub}}
1) bean to JSON
@Testpublic void Beantojson () {List List = new ArrayList (); List.add ("Xiaowang"); List.add ("Xiaohua"); Adress Adress = new Adress ("StreetName", "District"); Jsonobject jsonobject = jsonobject.fromobject (new User ("name", adress, list)); System.out.println (Jsonobject.tostring ());//console: {"adress": {"district": "District", "StreetName": "StreetName" }, "name": "Name", "Students": ["Xiaowang", "Xiaohua"]}}
2)
@Testpublic void List2json () {List List = new ArrayList (); List.add ("first"); List.add ("second"); Jsonarray Jsonarray = jsonarray.fromobject (list); System.out.println (Jsonarray.tostring ());} Public Map Getmap () {//declares a hash object and joins the data Map params = new HashMap ();p arams.put ("username", "username");p arams.put (" User_json "," user "); return params;} @Testpublic void Map2json () {Map map = Getmap (); Map map1 = Getmap (); List List = new ArrayList (); List.add (map); List.add (MAP1); Jsonarray Jsonarray = jsonarray.fromobject (list); System.out.println ("Jsonarray--->" + jsonarray.tostring ()); Jsonarray jsonArray1 = jsonarray.fromobject (map); System.out.println ("JsonArray1--->" + jsonarray1.tostring ());}
Complete:
public class Simpleexample {@Testpublic void List2json () {List List = new ArrayList (); List.add ("first"); List.add ("Second " ); Jsonarray Jsonarray = jsonarray.fromobject (list); System.out.println (Jsonarray.tostring ());} Public Map Getmap () {//declares a hash object and joins the data Map params = new HashMap ();p arams.put ("username", "username");p arams.put ("User_ JSON "," user "); return params;} @Testpublic void Map2json () {Map map = Getmap (); Map map1 = Getmap (); List List = new ArrayList (); List.add (map); List.add (MAP1); Jsonarray Jsonarray = jsonarray.fromobject (list); System.out.println ("Jsonarray--->" + jsonarray.tostring ()); Jsonarray jsonArray1 = jsonarray.fromobject (map); System.out.println ("JsonArray1--->" + jsonarray1.tostring ());} Parses the data and data objects included in the string according to the received JSON string @testpublic void Parsjsonfromfront () {//received jsonstring result = "[{\" username\ ": \" Your name\ ", \" user_json\ ": {\" username\ ": \" Your name\ ", \" nickname\ ": \" Your Nickname\ "}}]"; Generate JSON objects based on string Chen//based on string//The assumption is that the form of an array is converted to Jsona using Jsonarray.fromobject (Result)The Rray object//assumes that the JSON object is converted to Jsonobject object using Jsonobject.fromobject (result) Jsonarray resultarray = Jsonarray.fromobject ( result); Jsonobject jsonobject = resultarray.getjsonobject (0); Jsonobject User_json = Jsonobject.getjsonobject ("User_json");//value by key value string username = user_json.getstring (" Username "); String nickname = user_json.getstring ("nickname"); System.out.println ("username =" + username + ":" + "nickname =" + nickname);//console:username =your name:nickname = Your nickname} @Testpublic void Beantojson () {List List = new ArrayList (); List.add ("Xiaowang"); List.add ("Xiaohua"); Adress Adress = new Adress ("StreetName", "District"); Jsonobject jsonobject = jsonobject.fromobject (new User ("name", adress, list)); System.out.println (Jsonobject.tostring ());//console: {"adress": {"district": "District", "StreetName": "StreetName" }, "name": "Name", "Students": ["Xiaowang", "Xiaohua"]}}}
Related dependencies jar Download: http://download.csdn.net/detail/hao007cn/8995465
Simple use of Jsonarray and Jsonobject