Reece Cottam Reece Cottam - 10 months ago 73
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;


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 :)


This is the code I'm using on the client

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

//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;


catch (Exception ex)
// TODO: Log error

Answer Source

There are no extra quotes, the JSON data is valid (you can verify yourself by pasting the JSON data to 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.