1, in the server-side servlet class, you can use the following methods to collect data and generate the corresponding JSON string
Declare a hash object and add data
MAP params = new HashMap ();
Params.put ("username", username);
Params.put ("User_json", user);
Declaring a Jsonarray object and entering a JSON string
Jsonarray array = jsonarray.fromobject (params);
Put.println (Array.tostring ());
2, in the Web front end can be directly to the JSON string parsing through JavaScript, on the Android client, you need to use the JSON class to parse the string:
@description: Parses the data and data objects contained in a string based on the JSON string received
The received JSON string
String result = "[{\" username\ ": \" Your name\ ", \" user_json\ ": {\" username\ ": \" Your name\ ", \" nickname\ ": \" Your nickname \"}}]";
To generate a JSON object from a string
Jsonarray resultarray = new Jsonarray (result);
Jsonobject resultobj = resultarray.optjsonobject (0);
Get data item
String username = resultobj.getstring ("username");
Get Data Object
Jsonobject user = Resultobj.getjsonobject ("User_json");
String nickname = user.getstring ("nickname");
3. Conversion between data types
Import Org.json.JSONArray;
Import org.json.JSONException;
Import Org.json.JSONObject;
Do not forget to add the JSON package Oh!
public class Stringtojson {
public static void Main (string[] args) throws jsonexception{
SYSTEM.OUT.PRINTLN ("abc");
Defining JSON strings
String jsonstr = "{\" id\ ": 2," +
"\" title\ ": \" JSON title\ "," +
"\" config\ ": {" +
"\" width\ ": 34," +
"\" height\ ": 35," +
"}, \" Data\ ": [" +
"\" Java\ ", \" javascript\ ", \" Php\ "" +
"]}";
Convert to a Jsonobject object
Jsonobject jsonobj = new Jsonobject (JSONSTR);
Getting data from a Jsonobject object
JavaBean bean = new JavaBean ();
Gets the int type data according to the property name;
Bean.setid (Jsonobj.getint ("id"));
Gets the string data based on the property name;
Bean.settitle (jsonobj.getstring ("title"));
Get the Jsonobject class by property name
Jsonobject config = jsonobj.getjsonobject ("config");
Bean.setwidth (Config.getint ("width"));
Bean.setheight (Config.getint ("height"));
Get Jsonarray Array by property name
Jsonarray data = Jsonobj.getjsonarray ("Data");
for (int index = 0, length = data.length (), index < length; index++) {
Here in Org.json.JSONArray object actually did not find ToArray method, ask you to give a solution ah!
Bean.setlanguages (string[]);
}
}
}
Class javabean{
private int id;
Private String title;
private int width;
private int height;
Private string[] languages;
The setup and accessors are omitted here
}
4. JSON object converted to string string
public static void Main (string[] args) throws Jsonexception {
Create a Jsonobject object
Jsonobject json = new Jsonobject ();
Adding data to the JSON
Json.put ("username", "Wanglihong");
Json.put ("height", 12.5);
Json.put ("Age", 24);
Create a Jsonarray array and add the JSON to the array
Jsonarray array = new Jsonarray ();
Array.put (JSON);
Convert to String
String jsonstr = array.tostring ();
System.out.println (JSONSTR);
}
The resulting output is: [{"username": "Wanglihong", "height": 12.5, "Age": 24}]
5. Set conversion to Jsonarray object
public static void Main (string[] args) throws jsonexception{
Initialize the ArrayList collection and add data
list<string> list = new arraylist<string> ();
List.add ("username");
List.add ("Age");
List.add ("Sex");
Initializes the HashMap collection and adds an array
Map map = new HashMap ();
Map.put ("BookName", "CSS3 Combat");
Map.put ("Price", 69.0);
Initialize the Jsonarray object and add data
Jsonarray array = new Jsonarray ();
Array.put (list);
Array.put (map);
The resulting JSON string is: [[' Username ', ' age ', ' sex '],{' price ': ' bookname ': ' CSS3 Combat '}]
}
JSON in Java