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

前端 未结 5 1759
野性不改
野性不改 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:38

    Does this one help? I used it in my WebService to return Json content:

    private HttpContent ConvertToJsonContent(object content)
    {
      string jsonObject = JsonConvert.SerializeObject(content, Newtonsoft.Json.Formatting.Indented);
      return new StringContent(jsonObject, Encoding.UTF8, "application/json");
    }
    

    If strings have a "\" the two "\\" will come back. You can avoid this by using Unescape

    private HttpContent ConvertToJsonContent(object content)
    {
      string jsonObject = Regex.Unescape(JsonConvert.SerializeObject(content, Newtonsoft.Json.Formatting.Indented));
      return new StringContent(jsonObject, Encoding.UTF8, "application/json");
    }
    
    0 讨论(0)
  • 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<object>();
    var json = JsonConvert.SerializeObject(new { root = output }, Formatting.Indented);
    Response.Write(json);
    
    0 讨论(0)
  • 2020-12-15 18:00

    I suspect it's not actually adding escape characters at all. I suspect you're just looking at the string in a debugger, and that's adding the escaping.

    Try dumping it to a file or the console.

    0 讨论(0)
  • 2020-12-15 18:03

    I should note that you have not completely quoted the outputted stuff (I got the url to work in your answer - that should have been edited into your question rather than put as an answer). The string I got back in a file was this:

    "[{\"$id\":\"1\",\"CreationDate\":\"\\\/Date(1293186324257+0000)\\\/\",\"ImageUrl\":\"http:\/\/www.c-tina.com\/MyVoucherAdmin\/Images\/shop22\/burger.jpg\",\"Title\":\"Get one burger for free\",\"Description\":\"Bla lbzlfpzkfgmzke\\rdmjdgmj\\r\\r\\rlgfpzkegmkzepk\",\"ShopId\":22,\"PromotionId\":15,\"Shop\":null,\"Features\":[],\"SingleStats\":[],\"WhatsHots\":[],\"EntityKey\":{\"$id\":\"2\",\"EntitySetName\":\"Promotions\",\"EntityContainerName\":\"MyVoucherEntities\",\"EntityKeyValues\":[{\"Key\":\"PromotionId\",\"Type\":\"System.Int32\",\"Value\":\"15\"}]}}]"
    

    the key thing to me is that there are unescaped quotes at the front and end which makes me think that whatever is outputting it is deciding it needs to be quoted and if you are surrounding it in quotes you ahve to escape the quotes that are inside it.

    Without seeing the full output its hard to say if the problem is in teh code you've quoted above to generate the JSON or if there is a problem at a later step of processing this which is causing the quoting. Have you debugged and confirmed that the output of your serialize call is definitely producing the escaped version rather than it being done at a later stage potentially? If you're not used to the debugger then pay attention to Jon Skeet's suggest of dumping it to file or console to make sure there is no confusion that way.

    0 讨论(0)
  • 2020-12-15 18:04

    I found the reason why I had escape characters in my string ("\"). After serializing my objects, I am returning the JSON string to the client app through a WCF. Apparently, WCF is automatically adding these characters to the string before sending it to the network. It is a default behaviour and is apparently mandatory.

    As I didn't want these escape characters, the workaround is to change the return type of the service to Stream and so, returning your JSON string inside a memory stream. It works perfectly and is quite fast.

    0 讨论(0)
提交回复
热议问题