Java uses JSON for simple data encapsulation

Source: Internet
Author: User

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

Related Article

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.