JavaScriptSerializer().Serialize(实体框架对象)
也许,这对你来说并不是什么问题。但我第一次尝试 json 序列化。并阅读 stackowerflow 中的其他文章。
我已经创建了实体框架数据模型。 然后通过方法从对象获取所有数据:
private uqsEntities _db = new uqsEntities();
//get all data from table sysMainTableColumns where tableName=paramtableName
public List<sysMainTableColumns> getDataAboutMainTable(string tableName)
{
return (from column in _db.sysMainTableColumns
where column.TableName==tableName
select column).ToList();
}
我的webservice:
public string getDataAboutMainTable()
{
penta.DAC.Tables dictTable = new penta.DAC.Tables();
var result = dictTable.getDataAboutMainTable("1");
return new JavaScriptSerializer().Serialize(result);
}
和jQuery ajax方法
$('#loadData').click(function() {
$.ajax({
type: "POST",
url: "WS/ConstructorWS.asmx/getDataAboutMainTable",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#jsonResponse").html(msg);
var data = eval("(" + msg + ")");
//do something with data
},
error: function(msg) {
}
});
});
失败(来自fairbug):
missing ] after element list [Break on this error] var data = eval("(" + msg + ")");
ajax响应(如果我删除var data = eval(“(”+ msg +“)”,则通过Firebug) )
):
{"d":"[{\"ID\":1,\"TableName\":\"1\",\"Name\":\"d\",\"FullName\":\"f\",\"Type\":\"nvarchar(50)\",\"MeasurementUnit\":\"t \",\"EntityState\":2,\"EntityKey\":{\"EntitySetName\":\"sysMainTableColumns\",\"EntityContainerName\":\"uqsEntities\",\"EntityKeyValues\":[{\"Key\":\"ID\",\"Value\":1}],\"IsTemporary\":false}},{\"ID\":2,\"TableName\":\"1\",\"Name\":\"e\",\"FullName\":\"e\",\"Type\":\"int\",\"MeasurementUnit\":\"r \",\"EntityState\":2,\"EntityKey\":{\"EntitySetName\":\"sysMainTableColumns\",\"EntityContainerName\":\"uqsEntities\",\"EntityKeyValues\":[{\"Key\":\"ID\",\"Value\":2}],\"IsTemporary\":false}}]"}
数据有问题,代码在那里失败。我认为我没有很好地使用 JavaScriptSerializer().Serialize() 方法。
请告诉我,我在 C# 代码中犯了一个多大的错误?
May be, it is not so problematic for you. but i'm trying first time with json serialization. and also read other articles in stackowerflow.
I have created Entity Framework data model.
then by method get all data from object:
private uqsEntities _db = new uqsEntities();
//get all data from table sysMainTableColumns where tableName=paramtableName
public List<sysMainTableColumns> getDataAboutMainTable(string tableName)
{
return (from column in _db.sysMainTableColumns
where column.TableName==tableName
select column).ToList();
}
my webservice:
public string getDataAboutMainTable()
{
penta.DAC.Tables dictTable = new penta.DAC.Tables();
var result = dictTable.getDataAboutMainTable("1");
return new JavaScriptSerializer().Serialize(result);
}
and jQuery ajax method
$('#loadData').click(function() {
$.ajax({
type: "POST",
url: "WS/ConstructorWS.asmx/getDataAboutMainTable",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#jsonResponse").html(msg);
var data = eval("(" + msg + ")");
//do something with data
},
error: function(msg) {
}
});
});
Fails (from fairbug):
missing ] after element list [Break on this error] var data = eval("(" + msg + ")");
ajax Response (by Firebug if I remove var data = eval("(" + msg + ")")
):
{"d":"[{\"ID\":1,\"TableName\":\"1\",\"Name\":\"d\",\"FullName\":\"f\",\"Type\":\"nvarchar(50)\",\"MeasurementUnit\":\"t \",\"EntityState\":2,\"EntityKey\":{\"EntitySetName\":\"sysMainTableColumns\",\"EntityContainerName\":\"uqsEntities\",\"EntityKeyValues\":[{\"Key\":\"ID\",\"Value\":1}],\"IsTemporary\":false}},{\"ID\":2,\"TableName\":\"1\",\"Name\":\"e\",\"FullName\":\"e\",\"Type\":\"int\",\"MeasurementUnit\":\"r \",\"EntityState\":2,\"EntityKey\":{\"EntitySetName\":\"sysMainTableColumns\",\"EntityContainerName\":\"uqsEntities\",\"EntityKeyValues\":[{\"Key\":\"ID\",\"Value\":2}],\"IsTemporary\":false}}]"}
problem with data, code fails there. and i think i'm not use JavaScriptSerializer().Serialize() method very well.
Please, tell me, what a big mistake I made in C# code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
eval
。当您指定dataType: "json"
时,jQuery 会为您执行此操作。JavaScriptSerializer
将会死亡。d
!当根对象是数组时,WCF 服务会插入它来解决某些浏览器中的安全漏洞。eval
. jQuery does that for you when you specifydataType: "json"
JavaScriptSerializer
will die if one happens to contain a circular reference.d
! That's inserted by WCF services to work around a security hole in some browsers when the root object is an array.您是否尝试过使用 Firebug 或 Fiddler 进行调试以查看 JSON 内容是什么样的?
Have you tried debugging with Firebug or Fiddler to see what the JSON content looks like?