Json字符串是指符合json格式要求的字符串
Json对象是指符合json格式要求的对象
Json格式:
1、
var jsonstr={
"data":[
{"id":"123","name":"小李"},
{"id":"456","name":"小王"}
],
"total":2
}
取值:var a=jsonstr.data[0].name
赋值:jsonstr.data[0].name=a
.net将table 转化为json
/// <summary> /// 将dataTable列全部转换为json /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string DataTableToJson(DataTable dt) { StringBuilder sb = new StringBuilder(); sb.Append("["); for (int j = 0; j < dt.Rows.Count; j++) { sb.Append("{"); for (int k = 0; k < dt.Columns.Count; k++) { if (DBConvert.ToString(dt.Rows[j][k]).Contains("{")) continue;//排除json字段 sb.Append(""" + dt.Columns[k].ColumnName + "":"); sb.Append(""" + DBConvert.ToString(dt.Rows[j][k]) + "","); } if (sb.ToString().EndsWith(",")) { sb.Remove(sb.Length - 1, 1); } sb.Append("},"); } if (sb.ToString().EndsWith(",")) { sb.Remove(sb.Length - 1, 1); } sb.Append("]"); return sb.ToString(); }
JSON.parse()和JSON.stringify()
parse用于从一个字符串中解析出json对象,如
var str = '{"name":"huangxiaojian","age":"23"}'
结果:
JSON.parse(str)
Object
- age: "23"
- name: "huangxiaojian"
- __proto__: Object
注意:单引号写在{}外,每个属性名都必须用双引号,否则会抛出异常。
stringify()用于从一个对象解析出字符串,如
var a = {a:1,b:2}
结果:
JSON.stringify(a)
"{"a":1,"b":2}"
后台接受前台传过来的Json字符串:
JavaScriptSerializer jss = new JavaScriptSerializer();
BxLinkman blmnew = (BxLinkman)jss.Deserialize(rqt.Params["dataen"], typeof(BxLinkman));