In the process of network data transmission, it is often used to exchange data, JSON is a better data interchange format.
If the data to be exchanged are:
Username:welins
Password:123
Well, the format of the encapsulation should look like this:
{"username": "welins", "Password": "123"}
1, the sender should do this to encapsulate the data
Jsonobject json=new jsonobject ();
String username= "Welins";
String password= "123";
Json.put ("username", username ");
Json.put ("password", password);
Out.print (Json.tostring ());//Convert JSON object to string write to output stream
2, the receiver should be able to parse the data sent by imagining:
Gets the JSON string sent from the imagining from the input stream.
Bufferedread instr=new Bufferedread (Socket.getinputstream ());
Jsonobject json=new Jsonobject (). Fromobject (instr);//Convert the received JSON string to a JSON object
String username=json.getstring ("username");//Parse out from JSON object Userame
String password=json.getstring ("password");//Parse out from JSON object password
SYSTEM.OUT.PRINTLN (username);
System.out.println (Passworld);
Complete, this is the simplest form of JSON in Java!
Java uses JSON for simple data encapsulation