JSON principle Analysis and example introduction _javascript skill

Source: Internet
Author: User
This time in the project before and after the data interaction using the JSON, after this period of use, probably understand, a simple summary of JSON.
Json:javascript Object Notation (JavaScript object notation).
JSON is the syntax for storing and exchanging text information. Like XML.
JSON is smaller, faster, and easier to parse than XML.
Like XML, JSON is a data format based on plain text. Because JSON is inherently prepared for JavaScript, JSON's data format is very simple, you can transfer a simple string,number,boolean with JSON, or you can transfer an array or a complex object.
First look at a piece of code in the controller. The main view is to see how the data from the database is output in JSON format.
[Java]
Copy Code code as follows:

@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<istaffinfo> liststaffinfo = Sysstaffservice.getstaffbyplanid (Planid, Industryid);
for (int i=0;i<liststaffinfo.size (); 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 first putpututils
[Java]
Copy Code code as follows:

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 response PrintWriter and set the information to be output. The information returned by using JQuery's post judgment at the interface layer.
[JavaScript]
Copy Code code as follows:

<span style= "White-space:pre" > </span>function Distribute () {
var Dplanid = $ (". Currli"). attr ("id");
if (Dplanid!= "") {
$.ajax ({
Type: "POST",
URL: The action URL to do the validation,
DataType: "JSON",
Success:function (data) {
Have to assigned state
if (Data.result = = "Success" && data.personsituation = = "Unhave") {
With (document.getElementById ("planform")) {
action= the URL to be submitted after the validation is valid;
Method= "POST";
Submit ();
}
<span style= "White-space:pre" > </span>}

where Success:function (data) is a callback function, the action that is performed after the method that validates the action is successful. For more information on how to use jquery, click here to view.
About the post submission of jquery do not understand the classmate, click here to learn.
On the history of Ajax and jquery, it is recommended to see Wikipedia and write it clearly.
jquery has encapsulated the operation of fetching data from the response, so it is convenient to use here, eliminating the headaches of reading from 1.1 in XML, and bringing great convenience to development.

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.