在传统的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. github for windows 安装 使用

    遇到无数的未知问题.光是安装就搞了好久. 安装程序显示安装了.NET Framework4.5,然后提示重启.重启后,自动开始下载文件,最多到2%就走不动了. 后来请求了下面这个链接,才开始下载了(虽 ...

  2. linux(debian) arm-linux-g++ v4.5.1交叉编译 embedded arm 版本的QtWebkit (browser) 使用qt 4.8.6 版本

    最近需要做一个项目 在arm 架构的linux下 没有桌面环境的情况下拉起 有界面的浏览器使用. 考虑用qt 的界面和 qtwebikt 的库去实现这一系列操作. 本文参考: Qt移植到ARM Lin ...

  3. form操作

    1, /* 封装操作表单的常用方法 */ //获取要保存的数据 wjh 2015-10-22 function getSaveData(className) { if (className == nu ...

  4. WINFORM 输出txt文件

    SaveFileDialog saveFile1 = new SaveFileDialog(); saveFile1.Filter = "文本文件(.txt)|*.txt"; sa ...

  5. (原).NET程序加入多语言包解决方案工具,超级棒

    Multi-Language Add-In Version 5.04.0088 for Visual Studio 2013 安装包:http://www.jollans.com/SetupMulti ...

  6. js_截取Url值

    "total" -->传递参数时的名字 var reg = new RegExp("(^|&)" + "total" + &q ...

  7. 浏览器全屏事件(Html5)

    <button onclick="launchFullscreen(document.documentElement);"></button> functi ...

  8. JDBC增删改查简单测试

    首先编写一个entity以便与数据库表文件相对应 lyTable.java public class LyTable implements java.io.Serializable { private ...

  9. Git 常用命令大全

    Git常用操作命令: 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 查看远程仓库:$ git remote -v 添加 ...

  10. The Solution of UESTC 2016 Summer Training #1 Div.2 Problem C

    Link http://acm.hust.edu.cn/vjudge/contest/121539#problem/C Description standard input/output After ...