0

Below is MY DTO.

public class CustomerTO
{
  public int CustomerId { get; set;}
  public string CustName { get; set;}
  public string CustJson { get; set;}
}

Below is my LINQ to get Customer records.

var query = (from c in entities.tblCustomers
             select c).toList<CustomerTO>();

This returns the customer collection & in the UI I gets collection as.

Sample:-

    {  
    "CustomerId":113,
    "CustName":"Ram",
    "CustJson":""{\r\n  \"Number\": 143,\r\n  \"IsDeleted\": false,\r\n  \"GapAnalysisChecked\": false,\r\n  \"ShowGraphics\": true,\r\n  \"Impact\": {\r\n    \"Value\": \"DefaultNodeTitle_Impact\",\r\n    \"Details\": null,\r\n    \"DefaultValue\": \"DefaultNodeTitle_Impact\"}
   }

I'm in need to get a valid json string in CustJson varialble.

Please note that in the db, the stored data in CustJSON column is a valid json string.

So I tried this.

foreach(var cust in customers)
{
   if(cust.CustJson != null)
   {
     var parsedJson = JsonConvert.DeserializeObject(cust.CustJson); // this give a valid json
    cust.CustJson  = JsonConvert.SerializeObject(parsedJson);// but this creates a string with \r\n
   }
}

When I try this, in parsed JSON I get the desired JSON. However, when Serialize the parsed JSON again the same string returns.

How do I get the valid json string in Cust.Json??

Is there a better way to get the valid json without the foreach loop?

Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • I'm not entirely sure I understand the question but I think I have an idea. Are you inspecting the contents of the json variable using the debugger? Please be aware that the debugger will escape the string in order to make it legal C# syntax, this means that if the string contains a double quote character `"`, it will be prefixed with a backslash, since that is required when declaring a string literal containing a double backslash. Can you verify how you are looking at the variable? If possible, try writing the contents to a file and see if that looks OK. – Lasse V. Karlsen Feb 20 '17 at 07:39
  • @LasseV.Karlsen This DAL method is called from a service layer. All this data is exposed over WebAPI. I am using Postman to test – Kgn-web Feb 20 '17 at 07:47
  • 1
    Possible duplicate of [Serializing a list to JSON](http://stackoverflow.com/questions/9110724/serializing-a-list-to-json) – Masoud Andalibi Feb 20 '17 at 08:28
  • @Valkyriee, can you please explain how does my question is duplicate of that. That's a simple operation where you convert a list to json. – Kgn-web Feb 20 '17 at 08:57
  • Please post a [mcve] along with how you verify your output, what you observed and what you expected/wanted. – Lasse V. Karlsen Feb 20 '17 at 11:12

1 Answers1

0

Uhm, I think I got your problem. You want to include the JSON stored in the CustJson Column into the resulting json, on serializing CustomerTO. Right now this JSON is beeing escaped by the serializer because the serializer doesn't know that this is valid json.

What you are doing now is deserializing the json into a dynmaic object and then serialize it back to a json string into the very same property. Of course this will have no effect on serializing the whole object because the json will simply be escaped again.

You may want to enpower the magic of dynamic a little bit :)

var listWithDeserializedProperty = customers.Select(c => 
{
    dynamic cust = new System.Dynamic.ExpandoObject();
    cust.CustomerId = c.CustomerId;
    cust.CustName = c.CustName;
    if (!string.IsNullOrWitheSpace(c.CustJson))
        cust.CustJson = JsonConvert.DeserializeObject(c.CustJson);
    return cust;
});

return JsonConvert.SerializeObject(listWithDeserializedProperty);

If you now serialize this object, CustJson will be detected as an object and will be included into the resulting json as a whole without beeing escaped again. Of course you could also write a strongly typed view model for this, but for the sake of simplicity...

yan.kun
  • 6,820
  • 2
  • 29
  • 38
  • Nopes still same issue – Kgn-web Feb 20 '17 at 09:17
  • ` cust.CustJson = JsonConvert.DeserializeObject(c.CustJson);` this throws error. Cannot implicity convert object to string – Kgn-web Feb 20 '17 at 09:20
  • If I interpret your data correctly this should not happen, excpet for that JsonConvert.DeserializeObject() is not returning a deserialized object, but a simple string. Have you checked what is acuatlly beeing deserialized? – yan.kun Feb 20 '17 at 09:24
  • CustJson is nvarchar type in database & holds a valid json string – Kgn-web Feb 20 '17 at 09:28