Java code
- Public class User {
- Private String username;
- Private String password;
- public String getusername () {
- return username;
- }
- public void Setusername (String Username) {
- This.username = username;
- }
- public String GetPassword () {
- return password;
- }
- public void SetPassword (String password) {
- This.password = password;
- }
- }
Normal JavaBean (take user as Example) to JSON format
1. Turn into Jsonarray type
User user = new user ();
User.setusername ("cxl");
User.setpassword ("1234");
Jsonarray json = Jsonarray.fromobject (user);
System.out.println (json);//[{"password": "1234", "username": "cxl"}]
Response.getwriter (). Print (json.tostring ());
Fetch data in JS file
$.getjson ("http://localhost:8080/jQueryDemo/servlet/UserServlet", null,function (data) {
Alert (data[0].username);
Alert (data[0].password);
});
2. Turn into Jsonobject type
Jsonobject jsonobj = Jsonobject.fromobject (user);
System.out.println (jsonobj);//{"password": "1234", "username": "cxl"}
Response.getwriter (). Print (jsonobj);
Fetch data in JS file
Alert (data.username);
Alert (data.password);
List turns into JSON format
list<user> users = new arraylist<user> ();
User user = new user ();
User.setusername ("cxl");
User.setpassword ("1234");
User U = New user ();
U.setusername ("lhl");
U.setpassword ("1234");
Users.add (user);
Users.add (u);
1. Turn into Jsonarray type
Jsonarray json = Jsonarray.fromobject (users);
System.out.println (json.tostring ());
[{"password": "1234", "username": "cxl"},{"password": "1234", "username": "lhl"}]
Response.getwriter (). Print (json.tostring ());
JS data: alert (alert (data[0].username));
2. Turn into Jsonobject type
Do not use jsonobject jsonobj = Jsonobject.fromobject (users) directly;
This can be done in the following ways
Jsonobject jsonobj = new Jsonobject ();
Jsonobj.put ("users", users);
Jsonobj.put ("u", u);
System.out.println (jsonobj);
{"users": [{"password": "1234", "username": "cxl"},{"password": "1234", "username": "lhl"}],
"u": {"password": "1234", "username": "lhl"}}
Response.getwriter (). Print (jsonobj);
JS Fetch data: Alert (data.users[0].username);
Alert (data.u[0].username);
Map into JSON format
map<string,object> map = new Hashmap<string,object> ();
Map.put ("users", users);
Map.put ("u", u);
1. Turn into Jsonarray type
Jsonarray json = Jsonarray.fromobject (map);
System.out.println (json.tostring ());//
[{"users": [{"password": "1234", "username": "cxl"},{"password": "1234", "username": "lhl"}], "u": {"password": "1234", " Username ":" Lhl "}}]
Response.getwriter (). Print (json.tostring);
JS Fetch data: Alert (data[0].users[0].username);
2. Turn into Jsonobject type
Jsonobject json = Jsonobject.fromobject (map);
System.out.println (json);//
{"user": [{"password": "1234", "username": "cxl"},{"password": "1234", "username": "lhl"}], "u": {"password": "1234", " Username ":" Lhl "}}
Response.getwriter (). Print (json);
JS Fetch data: Alert (data.user[0].username);
Javabean,list,map turn into JSON format