JSON 语法:
- 数据在名称/值对中
- 数据由逗号分隔
- 花括号保存对象
- 方括号保存数组
js下 json格式
格式1、一个对象
var json= { "firstName":"John" , "lastName":"Doe" }
格式2、多个对象
var employees = [ { "firstName":"Bill" , "lastName":"Gates" }, { "firstName":"George" , "lastName":"Bush" }, { "firstName":"Thomas" , "lastName": "Carter" } ];
var name= employees[0].lastName;//获取值
employees[0].lastName = "Jobs";//修改值
格式3、对象 "employees" 是包含三个对象的数组
var txt = '{ "employees" : [' +
'{ "firstName":"Bill" , "lastName":"Gates" },' +
'{ "firstName":"George" , "lastName":"Bush" },' +
'{ "firstName":"Thomas" , "lastName":"Carter" } ]}';
格式4、杂揉对象
var testJson = { "Name" : "奥巴马" , "ByName" : ["小奥","小巴","小马"], "Education" : { "GradeSchool" : "华盛顿第一小学", "MiddleSchool" : ["华盛顿第一初中" , "华盛顿第一高中"], "University" : { "Name" : "哈佛大学", "Specialty" : ["软件工程","会计"] } } }
c# 下的json字符串
string testJson = "{"Name" : "奥巴马","ByName" : ["小奥","小巴","小马"],"Education":{"GradeSchool" : "华盛顿第一小学","MiddleSchool" : ["华盛顿第一初中" , "华盛顿第一高中"], "University" :{ "Name" : "哈佛大学", "Specialty" : ["软件工程","会计"]}}}";
栗子1:
事先定义好一个类
public class CatalogCustom { public string CatalogID { get; set; } public string DisplayName { get; set; } }
js下拼好正确的json字符串格式
$("[name=checkCatalog]").click(function(){ var CatalogID=$(this).val(); var DisplayName=$(this).attr("tag"); var json='{"CatalogID":"'+CatalogID+'","DisplayName":"'+DisplayName+'"}'; $.post("/GoWhere/AddCatalogOnly",{jsonStr:json},function(){ },"Text"); });
post到后台用string类型接收
public ActionResult AddCatalogOnly(string jsonStr) { CatalogCustom catalogCustom= SerializeHelper.DeserializeFromJson<CatalogCustom>(jsonStr); return View(); }
栗子2:
有时候我们会提交多组数据,此时我们可以用list集合来接收前台提交过来的数据,同时我们也要注意这种情况下前台json数据的拼接格式(上一个栗子可以看做是一个json字符串对象,现在则是多个json对象的字符串)
前台js:
$("#catalogSave").click(function(){ var array=[]; $("[name=checkCatalog]:checked").each(function(i,u){ var CatalogID=$(u).val(); var DisplayName=$(u).attr("tag"); array.push('{"CatalogID":"'+CatalogID+'","DisplayName":"'+DisplayName+'"}'); }); $.post("/GoWhere/AddCatalog",{str:"["+array.join(",")+"]"},function(msg){ if(msg=="ok"){ alert("编辑成功"); window.location.href="/GoWhere/GoWhereIndexLogin"; }else{ alert("编辑失败"); } },"text"); });
后台c#:
public ActionResult AddCatalog(string str) { List<CatalogCustom> list = SerializeHelper.DeserializeFromJson<List<CatalogCustom>>(str); int uid = SystemManager.GetUID(this.Request); int flag = GoWhereList.Instance.EditorProductUserLovesByUid(uid, list); if (flag > 0) { return this.Content("ok"); } else { return this.Content("ok"); } }
栗子3:后台传给前台json字符串,eval() 可用于将 JSON 文本转换为 JavaScript 对象
var txt = '{"employees":[' + '{"firstName":"Bill","lastName":"Gates" },' + '{"firstName":"George","lastName":"Bush" },' + '{"firstName":"Thomas","lastName":"Carter" }]}';
var obj = eval ("(" + txt + ")");
document.getElementById("fname").innerHTML=obj.employees[1].firstName
document.getElementById("lname").innerHTML=obj.employees[1].lastName
First Name: George
Last Name: Bush
栗子4:json对象转换成json字符串
var o = { name: "张三", age: 24 }; var str = JSON.stringify(o);
栗子5:
创建包含 JSON 语法的 JavaScript 字符串:
var txt = '{ "employees" : [' + '{ "firstName":"Bill" , "lastName":"Gates" },' + '{ "firstName":"George" , "lastName":"Bush" },' + '{ "firstName":"Thomas" , "lastName":"Carter" } ]}';
由于 JSON 语法是 JavaScript 语法的子集,JavaScript 函数 eval() 可用于将 JSON 文本转换为 JavaScript 对象。
eval() 函数使用的是 JavaScript 编译器,可解析 JSON 文本,然后生成 JavaScript 对象。必须把文本包围在括号中,这样才能避免语法错误:
var obj = eval ("(" + txt + ")");
在网页中使用 JavaScript 对象:
<p> First Name: <span id="fname"></span><br /> Last Name: <span id="lname"></span><br /> </p> <script type="text/javascript"> document.getElementById("fname").innerHTML = obj.employees[1].firstName document.getElementById("lname").innerHTML = obj.employees[1].lastName </script>
字典转Json
string json = (new System.Web.Script.Serialization.JavaScriptSerializer()).Serialize(dic);