Json usage principle and example

Source: Internet
Author: User
Json is used for data interaction in the front and back of the project this time. After this time, I will take a rough look and briefly summarize json. JSON: JavaScript Object Notation (JavaScriptObjectNotation ). JSON is the syntax for storing and exchanging text information. Similar to... SyntaxHighlighter.

Json is used for data interaction in the front and back of the project this time. After this time, I will take a rough look and briefly summarize json.
JSON: JavaScript Object Notation (JavaScript Object Notation ).
JSON is the syntax for storing and exchanging text information. Similar to XML.
JSON is smaller, faster, and easier to parse than XML.
Like XML, JSON is also based on plain text data format. Because JSON is designed for JavaScript, The JSON data format is very simple. You can use JSON to transmit a simple String, Number, or Boolean, or an array, or a complex Object.
First look at a piece of code in the controller. It mainly depends on how the data queried from the database is output in json format.
[Java]
@ RequestMapping ("/work/plan/checkSubmitForApproval ")
Public void checkSubmitForApproval (String planId, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
String result = "{\" result \ ": \" faild \ ", \" personSituation \ ": \" null \"}";
HttpSession session = request. getSession ();
String industryID = (String) session. getAttribute ("industryID ");
IIndustry industry = industryService. getById (industryID );
If (industry. getType (). equals ("XXX ")){
Try {
Boolean flag = false;
IProjectMain yearPlan = projectPlanService. findProjectPlanById (planId );
List ListStaffInfo = sysStaffService. getStaffByPlanId (planId, industryID );
For (int I = 0; I If (listStaffInfo. get (I). getPractitionersPost (). equals (StaffRole. PROGECTMANAGER. toString ())){
Flag = true;
}
}
If (flag = true ){
Result = "{\" result \ ": \" success \ ", \" personSituation \ ": \" "+ yearPlan. getPerson_Situation () + "\"}";
} Else {
Result = "{\" result \ ": \" success \ ", \" personSituation \ ": \" "+ yearPlan. getPerson_Situation () + "\", \ "isManager \": \ "false \"}";
}

} Catch (Exception e ){
Result = "{\" result \ ": \" falid \"}";
Throw new PlatformException (e );
} Finally {
OutputUtils. write (response, result, "text/x-json; charset = UTF-8 ");
}


Write code in PutputUtils:
[Java]
Public static void write (HttpServletResponse response, String text, String contentType)
{
PrintWriter out = null;
Response. setHeader ("Pragma", "No-cache ");
Response. setHeader ("Cache-Control", "no-cache ");
Response. setDateHeader ("Expires", 0 );
Response. setContentType (contentType );

Try
{
Out = response. getWriter ();
Out. write (text );
}
Catch (IOException e)
{
Logger. getLogger (OutputUtils. class). error (e. getMessage (), e );
} Finally {
If (out! = Null ){
Out. flush ();
Out. close ();
}
}
}

The idea is to get the printwriter of response and set the information to be output. The interface layer uses jquery's Post to determine the returned information.
[Javascript]
Function distribute (){
Var dplanId = $ (". currli"). attr ("id ");
If (dplanId! = ""){
$. Ajax ({
Type: "POST ",
Url: The action url for verification,
DataType: "json ",
Success: function (data ){
// HAVE is allocated
If (data. result = "success" & data. personSituation = "UNHAVE "){
With (document. getElementById ("planForm ")){
Action = url to be submitted after the verification is valid;
Method = "post ";
Submit ();
}
}
Success: function (data) is a callback function, that is, the operation performed after the action verification method is successful. Click here for details about how to use jquery.
If you do not understand jquery post submission, click here to learn.
For more information about the history of ajax and jquery, see Wikipedia.
Jquery has encapsulated the operations to retrieve data from response, so it is very convenient to use here, saving the headache of reading 1.1 points from xml and bringing great convenience to development.

 


 

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.