首页 > 代码库 > JSON学习总结

JSON学习总结

花括号保存对象方括号保存数组数组[];对象 {}数组对象:[ {},			{},			{}		  ](1)、复杂JSON对象。{"employees": [{ "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName":"Jones" }],"student":{  "name":"John Johnson",  "street":"Oslo West 16",   "age":33,  "phone":"555 1234567"}}C# 代码;class Employees_Object{  Empemployees[] employees;  Student student;  }(2)、单个对象var JSONObject = {  "name":"John Johnson",  "street":"Oslo West 16",   "age":33,  "phone":"555 1234567"};      C# 代码;  Class JSONObject  {     public string name{get;set;}	 public string street{get;set;}	 public string age{get;set;}	 public string phone{get;set;}  }   (3)、数组对象var employees = [{ "firstName" : "John" , "lastName" : "Doe" }, { "firstName" : "Anna" , "lastName" : "Smith" }, { "firstName" : "Peter" , "lastName" : "Jones" },  //特别注意此处的逗号;在json在线验证器中会出错。 ];  Empemployees[] employees=new Empemployees[]   //List<Empemployees> employees=new List<Empemployees>();{	new Empemployee{firstName="John",lastName="Doe"},	new Empemployee{firstName="John",lastName="Doe"},	new Empemployee{firstName="John",lastName="Doe"},};把 JSON 文本转换为 JavaScript 对象JSON 最常见的用法之一,是从 web 服务器上读取 JSON 数据(作为文件或作为 HttpRequest),将 JSON 数据转换为 JavaScript 对象,然后在网页中使用该数据。http://www.w3cschool.cc/json/json-eval.html由于 JSON 语法是 JavaScript 语法的子集,JavaScript 函数 eval() 可用于将 JSON 文本转换为 JavaScript 对象。eval() 函数使用的是 JavaScript 编译器,可解析 JSON 文本,然后生成 JavaScript 对象。必须把文本包围在括号中,这样才能避免语法错误:var obj = eval ("(" + txt + ")");