在一个MVC控制器有这样的代码
[HttpPost]
public JsonResult Gett_001_SQL()
{
//字符串赋值
string m = "{total: 9,page: 1,records: 17, rows : [ {id:'1', cell:[ '1','FG001','SFG001','1.000000','2013-04-02 0:00:00' ] },{id:'2', cell:[ '2','FG001','SFG002','1.000000','2013-02-01 0:00:00' ] } ] }";
var json = Json(m); //将字符串转换成JSON格式
return json; //返回给客户端
}
客户端接收到的信息为
"{total: 9,page: 1,records: 17, rows : [ {id:\u00271\u0027, cell:[ \u00271\u0027,\u0027FG001\u0027,\u0027SFG001\u0027,\u00271.000000\u0027,\u00272013-04-02 0:00:00\u0027 ] },{id:\u00272\u0027, cell:[ \u00272\u0027,\u0027FG001\u0027,\u0027SFG002\u0027,\u00271.000000\u0027,\u00272013-02-01 0:00:00\u0027 ] } ] }"
为什么会这样啊?
服务端要怎么修改才对啊?
[HttpPost]
public JsonResult Gett_001_SQL()
{
//字符串赋值
string m = "{total: 9,page: 1,records: 17, rows : [ {id:'1', cell:[ '1','FG001','SFG001','1.000000','2013-04-02 0:00:00' ] },{id:'2', cell:[ '2','FG001','SFG002','1.000000','2013-02-01 0:00:00' ] } ] }";
var json = Json(m); //将字符串转换成JSON格式
return json; //返回给客户端
}
客户端接收到的信息为
"{total: 9,page: 1,records: 17, rows : [ {id:\u00271\u0027, cell:[ \u00271\u0027,\u0027FG001\u0027,\u0027SFG001\u0027,\u00271.000000\u0027,\u00272013-04-02 0:00:00\u0027 ] },{id:\u00272\u0027, cell:[ \u00272\u0027,\u0027FG001\u0027,\u0027SFG002\u0027,\u00271.000000\u0027,\u00272013-02-01 0:00:00\u0027 ] } ] }"
为什么会这样啊?
服务端要怎么修改才对啊?
利用JavaScriptSerializer序列化类解析
JavaScriptSerializer ser = new JavaScriptSerializer();
组装好的对象 foo= ser.Deserialize<对象类型>(json_);
string m="{total: 9,page: 1,records: 17, rows : [ {id:1, cell:[ 1,FG001,SFG001,1.000000,2013-04-02 0:00:00 ] },{id:2, cell:[ 2,FG001,SFG002,1.000000,2013-02-01 0:00:00 ] } ] }"
var json = Json(m); //将字符串转换成JSON格式
return json; 客户端接收到的信息变成
"{total: 17,page: 1,records: 17, rows : [ {id:1, cell:[ 1,FG001,SFG001,1.000000,2013-04-02 0:00:00 ] } ] }"
客户端认为它是一个字符串,而不是JSON
服务端有进行转换的var json = Json(m); ===>这里就是在转换.
public ContentResult ActionName(){
return Content("{\"statusCode\":\"300\",\"message\":\"消息\"}");
}
是对象的话
public JsonResult ActionName(){
JsonResult jr = new JsonResult();
jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
jr.Data = obj;
return jr;
}
按你这样改更不行了
public JsonResult Gett_001_SQL()
{
//字符串赋值
string m = "{total: 9,page: 1,records: 17, rows : [ {id:'1', cell:[ '1','FG001','SFG001','1.000000','2013-04-02 0:00:00' ] },{id:'2', cell:[ '2','FG001','SFG002','1.000000','2013-02-01 0:00:00' ] } ] }";
var json = Json(m); //将字符串转换成JSON格式
JsonResult jr = new JsonResult();
jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
jr.Data = json;
return jr;
}客户端接收变成了
{"ContentEncoding":null,"ContentType":null,"Data":"{total: 9,page: 1,records: 17, rows : [ {id:\u00271\u0027, cell:[ \u00271\u0027,\u0027FG001\u0027,\u0027SFG001\u0027,\u00271.000000\u0027,\u00272013-04-02 0:00:00\u0027 ] },{id:\u00272\u0027, cell:[ \u00272\u0027,\u0027FG001\u0027,\u0027SFG002\u0027,\u00271.000000\u0027,\u00272013-02-01 0:00:00\u0027 ] } ] }","JsonRequestBehavior":1,"MaxJsonLength":null,"RecursionLimit":null}
var json = Json(m); ===>这行代码不就是将字符串转换成JSON对象吗?