Data transfer using JSON in Java __java

Source: Internet
Author: User

I have recently been working on a Web application based on the Java servlet and the development of the corresponding Anroid application client.

Among them, the more use of the JSON object to manipulate the formatted data in terms of access to the interface and the transfer of data: A JSON string is used on the server side to pass data and use JSON to parse the received data on the Web front-end or on the Android client.


First, using JSON in Java requires the introduction of a Org.json package (click here to download the appropriate Jar pack.) and introduce the corresponding JSON class in the program:

1 import org.json.JSONArray;
2 import org.json.JSONException;
3 Import Org.json.JSONObject;

Second, in the server-side servlet class, you can collect data and generate the corresponding JSON string using the following methods

1//Declare a hash object and add Data
2 Map params =  new HashMap ();
3 
4 params.put ("username", username);
5 Params.put ("User_json", user);
6 
7//Declare the Jsonarray object and enter the JSON string
8 Jsonarray array = jsonarray.fromobject (params);
9 Put.println (array.tostring ());

In the Web front-end, you can parse the JSON string directly through JavaScript, and in the case of the Android client, you need to use the JSON class to parse the string:

1//@description: Parse the data contained in the string and the data Object 2 3/receive the 
 JSON string
 4 string result = "[{\" username\ ": \"] According to the received JSON string Your name\ ", \" user_json\ ": {\" username\ ": \" Your name\ ", \" nickname\ ": \" Your Nickname\ "}]";
 5 
 6//Generate JSON object based on string
 7 Jsonarray resultarray = new Jsonarray (result);
 8 Jsonobject resultobj = resultarray.optjsonobject (0);
 9 
10//Get data Item one
String username = resultobj.getstring ("username"); 
13//Get Data object
jsonobject user = Resultobj.getjsonobject ("User_json");
String nickname = user.getstring ("nickname");

In fact, the main concern is the conversion between the following set of data types:

1. String converted to JSON object

 1 Import Org.json.JSONArray;
 2 Import org.json.JSONException;
 3 Import Org.json.JSONObject;
 4 5//Don't forget to add the JSON package Oh! 6 public class Stringtojson {7 public static void Main (string[] args) throws jsonexception{8 9 S
YSTEM.OUT.PRINTLN ("abc"); 10//Definition JSON string one string jsonstr = "{\ id\": 2, "+" \ "title\": \ "JSON title\", "+ 1 3 "" "config\": {"+" "width\": "," + "\" height\ ": 35," + 1 
6 "}, \" Data\ ": [" + "\" Java\ ", \" javascript\ ", \" Php\ "" + 18 "]}";
19 20//Convert to Jsonobject object Jsonobject jsonobj = new Jsonobject (JSONSTR);
22 23//Get data from Jsonobject object JavaBean bean = new JavaBean ();
25 26//To obtain int type data according to the attribute name;     
Bean.setid (Jsonobj.getint ("id"));
28 29//To get string data based on property name; Bean.settitle (jsonobj.getstring ("TitLe "));
31 32//According to the property name to get Jsonobject class Jsonobject config = jsonobj.getjsonobject ("config");
Bean.setwidth (Config.getint ("width"));
Bean.setheight (Config.getint ("height"));
36 37//Jsonarray array Jsonarray data = Jsonobj.getjsonarray ("data") according to the property name; for (int index = 0, length = data.length (); index < length; index++) {40//here in Org.json.JSONArray
The object actually did not find ToArray method, please friends to give a solution ah.
A//Bean.setlanguages (string[]); The Javabean{class is the private int ID, the private String title, and the Priv
ate int width;
a private int height;
Wuyi Private string[] languages; 52 53//This omits the setup and accessor 54}

2. JSON object converted to string string

1 public static void main (string[] args) throws Jsonexception {
 2         
 3         //Create Jsonobject Object
 4         jsonobject JSON = new Jsonobject ();
 5         
 6         //Add data to JSON
 7         json.put ("username", "Wanglihong");
 8         json.put ("height", 12.5);
 9         json.put ("Age",);
One         //Create Jsonarray array and add JSON to array         jsonarray array = new
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.