JavaScriptSerializer (). Serialization (Object of the Framework object)

Perhaps this is not so problematic for you. but I'm trying for the first time with json serialization. as well as read other articles on stackoverflow.

I created an Entity Framework data model. then using the method to get all the data from the 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 web service:

 public string getDataAboutMainTable() { penta.DAC.Tables dictTable = new penta.DAC.Tables(); var result = dictTable.getDataAboutMainTable("1"); return new JavaScriptSerializer().Serialize(result); } 

and 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) { } }); }); 

Doesn't work (from fairbug):

 missing ] after element list [Break on this error] var data = eval("(" + msg + ")"); 

ajax Response (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}}]"} 

data problem, the code does not work there. and I think that I am not using JavaScriptSerializer (). The Serialize () method is very good.

Please tell me what a big mistake I made in C # code?

+1
json c # entity-framework jsonserializer
source share
2 answers
  • You do not need eval . jQuery does this for you when you specify dataType: "json"
  • It's a good idea to serialize entities directly, because JavaScriptSerializer will die if one of them contains a circular reference.
  • Do not forget d ! This is added by WCF services for working with a security hole in some browsers when the root object is an array.
+3
source share

Have you tried debugging Firebug or Fiddler to see what the contents of JSON look like?

0
source share

Source: https://habr.com/ru/post/650415/


All Articles