在传统的Web开发过程中,前端工程师或者后台工程师会在页面上写后台的相关代码,比如在ASP.NET MVC4里面写如下代码:

@Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })

.csharpcode, .csharpcode pre

{

font-size: small;

color: black;

font-family: consolas, "Courier New", courier, monospace;

background-color: #ffffff;

/white-space: pre;/

}

.csharpcode pre { margin: 0em; }

.csharpcode .rem { color: #008000; }

.csharpcode .kwrd { color: #0000ff; }

.csharpcode .str { color: #006080; }

.csharpcode .op { color: #0000c0; }

.csharpcode .preproc { color: #cc6633; }

.csharpcode .asp { background-color: #ffff00; }

.csharpcode .html { color: #800000; }

.csharpcode .attr { color: #ff0000; }

.csharpcode .alt

{

background-color: #f4f4f4;

width: 100%;

margin: 0em;

}

.csharpcode .lnum { color: #606060; }

这样的代码有优点,当然也有缺点。缺点就是前后端分离的不彻底,前端工程师需要了解一些后台的代码,而已在不能把前端和后台都分开开发和部署。采用JSON就可以解决这一问题。就是前端和后台交换数据的格式都采用JSON。JSON:JavaScript Object Notation的缩写,是一种基于JavaScript的字面量表达式的数据格式类型。在ECMAScript第5版标准中也包含了JSON这一类型。

JSON能够通过4种基本数据类型以及2种数据结构化数据类型来表示。4种基本数据类型是:字符串值型,数值型,布尔型和null型。结构化数据类型是对象和数组这两种。举例如下:

  <td valign="top" width="200"><font size="3">举例</font></td>
</tr> <tr>
<td valign="top" width="200"><font size="3">字符串</font></td> <td valign="top" width="200"><font size="3">“test”</font></td>
</tr> <tr>
<td valign="top" width="200"><font size="3">数值</font></td> <td valign="top" width="200"><font size="3">123</font></td>
</tr> <tr>
<td valign="top" width="200"><font size="3">布尔型</font></td> <td valign="top" width="200"><font size="3">true或者false</font></td>
</tr> <tr>
<td valign="top" width="200"><font size="3">null值</font></td> <td valign="top" width="200"><font size="3">null</font></td>
</tr> <tr>
<td valign="top" width="200"><font size="3">对象</font></td> <td valign="top" width="200"><font size="3">{&quot;x&quot;:1,&quot;y&quot;:2,&quot;val&quot;:&quot;foobar&quot;}</font></td>
</tr> <tr>
<td valign="top" width="200"><font size="3">数组</font></td> <td valign="top" width="200"><font size="3">{1,2,true,'hello'}</font></td>
</tr>
数据类型

 

开发过程中,很多操作都包含了JSON格式数据类型的字符串与JavaScript对象间的相互转换。在Ajax提交表单时,需要将内部的对象转换JSON字符串之后再传输。而在接收JSON数据端,需要先将JSON字符串转换为JavaScript对象之后,才能不借助第三方类库对其值进行操作。在浏览器不支持JSON.stringify()和JSON.parse()方法之前,开发者都会使用json2.js在前端处理JSON字符串和JSON对象之间的转换。下面举例说明,将JSON字符串转换对象,将对象转换为JSON字符串。

//将JSON字符串转换对象

var s='{"x":1,"y":2,"val":"foobar"}';
var obj=JSON.parse(s);

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

//将对象转换JSON字符串

var test=JSON.stringify({x:1,y:2,val:'foobar'});
console.log(test);

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

在实际开发过程中,经常使用JSON数组,和包含数组的JSON,如下代码所示:

var result=JSON.parse('[{"name":"aaa","age":"20","gender":"female"},{"name":"bbb","age":"21","gender":"male"},{"name":"ccc","age":"22","gender":"male"}]');

            for(var index in result){
document.write(result[index].name);
} var result1=JSON.parse('{"counts":5,"items":["aaa","bbb","ccc","ddd"]}');//包含数组的JSON字符串
console.log(result1.counts); for(var index in result1.items){
console.log(result1.items[index]);
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

以上仅讨论客户端,也就是浏览器里面如何JSON,在后台开发过程中,也需要解析JSON,或者把C#,PHP或者Java的数据内容生成JSON。下一篇博客主要介绍这个内容。

参考网址:https://developer.mozilla.org/zh-CN/docs/JSON

JSON格式验证:http://jsonlint.com/

JSON入门:http://www.ibm.com/developerworks/cn/web/wa-lo-json/

JSON入门指南--客户端处理JSON的更多相关文章

  1. JSON入门指南

    JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,很适合于server与 JavaScript 的交互.本文将高速解说 JSON 格式.并通过代码演示样 ...

  2. [转]JSON 入门指南

    原文地址:http://www.ibm.com/developerworks/cn/web/wa-lo-json/ 尽管有许多宣传关于 XML 如何拥有跨平台,跨语言的优势,然而,除非应用于 Web ...

  3. JSON入门指南--服务端处理JSON

    平时公司使用的ASP.NET MVC3来开发Web项目,其实在ASP.NET中已经原生的支持JSON.所以基本不需要引进Newtonsoft.Json.dll.下面看在MVC4中,后台生成JSON数据 ...

  4. JSON取代XML?--JSON入门指南

    定义 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C# ...

  5. 【译】JWT(JSON Web Token) 入门指南

    JWT 入门指南 原文地址:https://blog.angular-university.io/angular-jwt/ 这篇文章是两篇手把手教你如何在Angular应用(也适用于企业级应用)中实现 ...

  6. JSON风格指南-真经

    简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语 ...

  7. JSON风格指南

    中文版:https://github.com/darcyliu/google-styleguide/blob/master/JSONStyleGuide.md 英文版:https://google.g ...

  8. SpringMVC客户端发送json数据时报400错误

    当测试客户端发送json数据给服务器时,找不到响应路径? 原来是参数类型不符,即使是json也要考虑参数的个数和类型 解决:将age请求参数由"udf"改为"3" ...

  9. JSON入门教程

    尽管有许多宣传关于 XML 如何拥有跨平台,跨语言的优势,然而,除非应用于 Web Services,否则,在普通的 Web 应用中,开发者经常为 XML 的解析伤透了脑筋,无论是服务器端生成或处理 ...

随机推荐

  1. Tomcat7安装配置 for Ubuntu

    一.环境说明: 操作系统:Ubuntu 12.04.2 LTS Tomcat:apache-tomcat-7.0.52 二.下载 下载地址:http://tomcat.apache.org/ 这里下载 ...

  2. String对象方法扩展

    /** *字符串-格式化 */ String.prototype.format = function(){ var args = arguments;//获取函数传递参数数组,以便在replace回调 ...

  3. js实现div居中

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. WinForm窗体 SSK 界面的使用

    1.下载 SSk文件 找到  IrisSkin2.dll 文件和.SSK结尾的文件    为了防止万一粘贴到项目的bin>Debug下面 2. 打开工具箱   添加一个选项卡 3. 添加选择项& ...

  5. 用C#语言在Visual Studio 2010里开发一个自定义的PowerShell Cmdlet

    1. 打开Visual Studio 2010 2. 新建一个基于Class Library的项目 3. 给项目起个名字然后OK 4. 为项目添加下列Reference System.Manageme ...

  6. 多位数每一位个系数:个位num%10;十位num/10%10.......

    请输出满足这样条件的五位数. 个位=万位 十位=千位 个位+十位+千位+万位=百位 思路: 1.定义一个要操作的五位数变量num 2.求出每一位个系数 个:num%10 十:num/10%10 百:n ...

  7. AngularJS 中的Promise --- $q服务详解

    先说说什么是Promise,什么是$q吧.Promise是一种异步处理模式,有很多的实现方式,比如著名的Kris Kwal's Q还有JQuery的Deffered. 什么是Promise 以前了解过 ...

  8. (源码下载)高灵活度,高适用性,高性能,轻量级的 ORM 实现

    我在上一篇博客中简单说明了一个面向内存数据集的“ORM”的实现方法,也提到我的设计实现或许不能称之为“ORM”,姑且称之为 S-ORM吧. 可能有些小伙伴没有理解我的思路和目的,与传统ORM框架做了简 ...

  9. New Career

    Today I received the official confirmation letter with very good grade for the probation, it means t ...

  10. 一则spring容器启动死锁问题(DefaultListableBeanFactory/DefaultSingletonBeanRegistry)

    线上发现一个问题,应用在启动时会卡死,log上并没有什么异常输出,初判应该是死锁问题. 抓现场的thread dump文件, 确实是有两个线程有deadlock问题. 线程一 "HSFBiz ...