Home  >  Article  >  Web Front-end  >  json principle analysis and example introduction_javascript skills

json principle analysis and example introduction_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:47:451220browse

This time, json was used in the data interaction between the front and backend of the project. After this period of use, I have a general understanding and a brief summary of json.
JSON: JavaScript Object Notation.
JSON is a syntax for storing and exchanging textual information. Similar to XML.
JSON is smaller, faster, and easier to parse than XML.
Like XML, JSON is also a plain text-based data format. Since JSON is inherently prepared for JavaScript, the data format of JSON is very simple. You can use JSON to transmit a simple String, Number, Boolean, an array, or a complex Object.
First look at a piece of code in the controller. The main thing to look at is to see how the data queried from the database is output in json format.
[java]

Copy code The code is 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 listStaffInfo = sysStaffService. getStaffByPlanId(planId, industryID);
for(int i=0;iif(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");
}

First enter PutputUtils Write code :
[java]
Copy code The code is 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 printwriter of the response and set the information to be output into it. Use jquery's Post at the interface layer to determine the returned information.
[javascript]
Copy code The code is as follows:

function distribute(){
var dplanId = $(".currli").attr("id");
if(dplanId != "") {
$.ajax({
type : "POST",
url : action url for verification,
dataType : "json",
success : function(data) {
//HAVE is assigned status
if (data.result == "success" && data.personSituation == "UNHAVE") {
with (document.getElementById("planForm")) {
action=URL to be submitted after verification;
method="post";
submit();
}
}

Where success: function(data) is a callback function, which is the operation performed after the above verification action method is successful. Click here to view the details of how to use jquery. >For students who don’t understand jquery’s post submission, click here to learn.
For the history of ajax and jquery, it is recommended to refer to Wikipedia, which is very clearly written.
jquery has already encapsulated the data from the response. Operation, so it is very convenient to use here, eliminating the headache of reading bit by bit from xml, which brings great convenience to development
.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn