Reece Cottam Reece Cottam - 1 month ago 12
JSON Question

How can I escape double quotes in my JSON responses?

I have the below code which transforms a datatable into a JSON string, the JSON I'm getting from my WCF service looks like this;

[{"id":"7c6c2d6b-416c-4535-95ba-b49a9f0c008b","title":"hello","description":"world","complete":true,"DependsOnTask":"63d47a78-ba4d-4740-9f61-9247dd9d5e82","id1":"63d47a78-ba4d-4740-9f61-9247dd9d5e82","title1":"another","description1":"example","complete1":false,"DependsOnTask1":null}]


and the conversion code looks like this;

string json = JsonConvert.SerializeObject(dt,Formatting.Indented);
string cleanjson = json.Replace("\"", "");
return json;


the problem is that I can't deserialise the JSON properly on the client recieving that response because the JSON has extra double quotes in it, and I have been unable to remove them using string.replace.

is there another way to do this at all?

Thanks in advance :)

*******UPDATE*********

This is the code I'm using on the client

private void LoadTasks()
{
// get the todo list items
ToDoService.ToDoServiceClient client = new ToDoService.ToDoServiceClient();

try
{
//List<ToDoService.ToDoItemContract> toDoItems = client.GetToDoItems("").ToList();
//List<string> ItemsWithDependantTasks = new List<string>();

var Items= client.GetAllDependantTasks();
List<string> list = JsonConvert.DeserializeObject<List<string>>(Items);



dlTasks.DataSource = list;


dlTasks.DataBind();

client.Close();
}
catch (Exception ex)
{
// TODO: Log error
client.Abort();
}
}

Answer

There are no extra quotes, the JSON data is valid (you can verify yourself by pasting the JSON data to https://jsonformatter.curiousconcept.com/ for instance).

However, how do you expect JSON.NET to deserialize that into a list of plain strings? This needs to be deserialized to some dictionary, a JObject or an entity with matching properties.