json原理分析及实例介绍
json原理分析及实例介绍
发布时间:2016-12-30 来源:查字典编辑
摘要:这次在项目中前后台的数据交互中用到了json,经过这段时间的使用,大概了解了一下,简单总结一下json。JSON:JavaScript对象表...

这次在项目中前后台的数据交互中用到了json,经过这段时间的使用,大概了解了一下,简单总结一下json。

JSON:JavaScript 对象表示法(JavaScript Object Notation)。

JSON 是存储和交换文本信息的语法。类似 XML。

JSON 比 XML 更小、更快,更易解析。

和 XML 一样,JSON 也是基于纯文本的数据格式。由于 JSON 天生是为 JavaScript 准备的,因此,JSON 的数据格式非常简单,您可以用 JSON 传输一个简单的 String,Number,Boolean,也可以传输一个数组,或者一个复杂的 Object 对象。

先看controller中的一段代码。看主要是看从数据库查询出来的数据是怎样以json的格式输出的。

[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<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");

}

先PutputUtils中的write代码:

[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();

}

}

}

其中的思路是得到response的printwriter,将要输出的信息设置到其中。在界面层利用jquery的Post判断返回的信息。

[javascript]

复制代码 代码如下:

<span> </span>function distribute(){

var dplanId = $(".currli").attr("id");

if(dplanId != ""){

$.ajax({

type : "POST",

url :做验证的action url,

dataType : "json",

success : function(data) {

//HAVE为已分配状态

if (data.result == "success" && data.personSituation == "UNHAVE") {

with (document.getElementById("planForm")) {

action=验证合法后要提交的url;

method="post";

submit();

}

<span> </span>}

其中success:function(data)是一个回调函数,即上面做的验证action的方法成功之后执行的操作。在jquery的使用方法详情点击这里查看。

关于jquery的post提交不理解的同学,点击这里学习。

关于ajax和jquery的历史,建议参见维基百科中,写的很清楚。

jquery已经封装好了从response中取data的操作,所以这里用起来非常方便,省去了从xml中一点一点读取的头疼,给开发带来了极大方便。

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Javascript教程学习
热门Javascript教程学习
编程开发子分类