博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
json具体解释
阅读量:4324 次
发布时间:2019-06-06

本文共 1540 字,大约阅读时间需要 5 分钟。

function testJson() {	var jsonData = {		"firstName" : "John",		"lastName" : "Doe",		"age" : 23	};	var employees = {		"accounting" : [// accounting is an array in employees.		{			"firstName" : "John", // First element			"lastName" : "Doe",			"age" : 23		}, {			"firstName" : "Mary", // Second Element			"lastName" : "Smith",			"age" : 32		}], // End "accounting" array.		"sales" : [// Sales is another array in employees.		{			"firstName" : "Sally", // First Element			"lastName" : "Green",			"age" : 27		}, {			"firstName" : "Jim", // Second Element			"lastName" : "Galley",			"age" : 41		}] // End "sales" Array.	}// End Employees	alert(employees.sales[1].firstName);//	alert(employees.sales[1]["lastName"]);}

通过AJAX接收JSON数据

通过AJAX接收JSON数据有三个不同方式.委派,回调与解释.
通过委派得到JSON
 
这种方法没有标准命名约定,只是"委派法"倒是一个挺好的描写叙述名字,由于server创建的javascript表达式文件会把JSON分派到一个变量 中.当把server的返回文本作为參数传给eval函数时,someVar变量就会装载JSON对象,然后你就能够通过这个变量訪问.
var JSONFile = "someVar = { 'color' : 'blue' }";  // example of what is received from the server.服务器返回数据演示样例
eval(JSONFile); // Execute the javascript code contained in JSONFile.运行JSONFile中的javascript代码.
document.writeln(someVar.color); // 输出'blue'
 
通过回调得到JSON
 
第二个方法预先定义一个以JSON数据作为參数的函数,然后server返回的javascript表达式中调用这个函数.这种方法叫"回调法".这个方式被广泛地应用在处理第三方JSON数据中(比如,从其他域名获取的JSON数据)
function processData(incommingJSON) {
   document.writeln(incommingJSON.color); // 输出'blue'
}
// example of what is received from the server...
var JSONFile = "processData( { 'color' : 'blue' } )";
eval(JSONFile);
 

 

转载于:https://www.cnblogs.com/zfyouxi/p/5210782.html

你可能感兴趣的文章
2010年ImagineCup,我们共同走过
查看>>
代码片段收集
查看>>
vue-cli3创建项目时报错
查看>>
输入1-53周,输出1-53周的开始时间和结束时间
查看>>
实验二
查看>>
shell——按指定列排序
查看>>
crash 收集
查看>>
Oracle数据库索引使用及索引失效总结
查看>>
507 LOJ 「LibreOJ NOI Round #1」接竹竿
查看>>
UI基础--烟花动画
查看>>
hibernate 批量插入数据
查看>>
2018. 2.4 Java中集合嵌套集合的练习
查看>>
精通ASP.NET Web程序测试
查看>>
vue 根据不同属性 设置背景
查看>>
51Nod1601 完全图的最小生成树计数 Trie Prufer编码
查看>>
Codeforces 1110D. Jongmah 动态规划
查看>>
android驱动在win10系统上安装的心酸历程
查看>>
优雅的程序员
查看>>
oracle之三 自动任务调度
查看>>
Android dex分包方案
查看>>