Java Processing JSON data

Source: Internet
Author: User

There are two types of structures in the JSON structure:

JSON is simply called objects and arrays in JavaScript, so these two structures are objects and arrays of two structures that can represent a variety of complex structures.

1. Object: The object in JS is represented as "{}" in the content, the data structure is {Key:value,key:value,...} The structure of key-value pairs, in object-oriented languages, key is the property of the object, value is the corresponding property value, so it is easy to understand that the value method is the object. Key Gets the property value, the type of the property value can be a number, a string, an array, several objects.

2, array: The array in JS is the middle bracket "[]" in the content, data structure for ["Java", "JavaScript", "VB",...], the value way and all languages, using index get, the type of field value can be number, string, array, object several.

Through the object, the array 2 kinds of structure can be combined into a complex data structure.

Different JSON can use different processing methods to simplify the steps, the following examples illustrate the various processing methods.

Add the Json-lib-2.4-jdk15.jar package first, and add the other five required packages at the same time:

Commons-beanutils-1.8.0.jar

Commons-collections-3.1.jar

Commons-lang-2.5.jar

Commons-logging-1.1.1.jar

Ezmorph-1.0.6.jar

The JSON data can be converted to map data for processing without knowing the key in the JSON data:

var Requestjson = {

"Key1": ["Data1", "Data2", "Data3", "data4"],

"Key2": ["Data1", "Data2", "Data3", "data4"],

"Key2": ["Data1", "Data2", "Data3", "data4"],

....

}

var jsondata = {

"Jsondata": Requestjson

}

Foreground Ajax Code

$.ajax ({url: "Ajaxservlet", type: "POST", Data:jsondata,success:function (ResponseText, Textstatus) {$ ("#resText"). Text (responsetext);},error:function () {alert ("error");}});


The following function enumerates a JSON data processing method from the foreground via AJAX to the background servlet, since the background accepts the data and does not know the exact key value, so the foreground sends the Requestjson data into a key-value pair before it is sent.

protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { String data = Request.getparameter ("Jsondata");//parsing JSON data jsonobject jsondata = jsonobject.fromobject (data);  map<string, object> map = Jsondata; Response.setcontenttype ("text/html"); PrintWriter writer = Response.getwriter (); for (entry<string, object> entry:map.entrySet ()) {writer.println ( Entry.getkey () + "=" +entry.getvalue ());}}


PS: in this sowing JSON format directly into the background, through Request.getparameternames (); Names value is key1[],key2[],key3[] ... Instead of Key1,key2,key3 .... If the value after Key1 is a string, the obtained value is key1.

var Requestjson = {

"Key1": ["Data1", "Data2", "Data3", "data4"],

"Key2": ["Data1", "Data2", "Data3", "data4"],

"Key2": ["Data1", "Data2", "Data3", "data4"],

....

}


Java Processing JSON data

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.