Example of data transfer using JSON in Java
Recently, I was working on the development of a web application based on java servlet and the corresponding anroid application client.
Among them, in terms of interface access and data transmission, JSON objects are mostly used to operate formatted data: JSON strings are used to transfer data on the server side, and JSON is used to parse the received data on the web front end or Android client.
First, using JSON in Java requires the introduction of org JSON package and introduce corresponding JSON classes into the program:
Secondly, in the servlet class on the server side, you can use the following methods to collect data and generate the corresponding JSON string
On the web front end, you can directly parse JSON strings through JavaScript. On the Android client, you need to use JSON classes to parse strings:
In fact, it mainly involves the conversion between the following centralized data types:
1. Convert string to JSON object
2. Convert JSON object to string
The final output result is: [{"username": "wanglihong", "height": 12.5, "age": 24}]
3. Convert collection to jsonarray object
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.