Json.Net unexpected characters (“\”) when serializing my entities

前端 未结 5 1766
野性不改
野性不改 2020-12-15 17:36

I am using the excellent Json.Net library to serialize my entities generated by entity framework. I use the following code to do so :

using (MyVoucherEntitie         


        
5条回答
  •  情歌与酒
    2020-12-15 17:46

    It's invalid JSON because the result of serializing a list of objects is an array, i.e., the json will start with a [ and ending with a ]. To fix this, you need to wrap the list of objects in a root object (any instance of a class or an anonymous object), so, the resulting string will start with a { and end with }.

    For example:

    var output = new List();
    var json = JsonConvert.SerializeObject(new { root = output }, Formatting.Indented);
    Response.Write(json);
    
        

    提交回复
    热议问题