Reputation: 49
By using below code i am calling api and trying to deserialize. I have replaced string to json format as like below,
//Code to get string response from api
var client = new WebClient();
client.Credentials = new NetworkCredential(userName, passWord);
client.Headers.Add(headerKey, headerValue);
client.QueryString.Add("companynumber", companyNumber);
var response = client.DownloadString(customerAPI);
response = response.Replace("\"", "").Replace("\\", "\"");
var results = JsonConvert.DeserializeObject<List<ProjectResponse>>(response);
output json result
"\"[{\\"data\\":{\\"closed\\":false,\\"companynumber\\":\\"430\\",\\"customernumber\\":\\"430\\\\"INTERNAL\\"}},{\\"data\\":{\\"closed\\":false,\\"companynumber\\":\\"430\\",\\"customernumber\\":\\"430INTERNAL\\"}}]"
And the respective deserialize class is below,
public class Project
{
public Nullable<long> CompanyNumber { get; set; }
public string CustomerNumber { get; set; }
public bool Closed { get; set; }
}
public class ProjectResponse
{
public Project data { get; set; }
}
In this example i am getting two list, in the first one customernumber has \\"430\\\\"INTERNAL\\ double quotes and backslash value so this is not allowing me deserialize to my respective class.so how to deserialize string eventhough one of the column has double quotes in it.
Upvotes: 1
Views: 321
Reputation: 247333
The code has be serialized twice. A model was serialized to JSON and then the JSON string was serialized again.
To fix you need to do the reverse.
var response = client.DownloadString(customerAPI);
var json = JsonConver.DeserializeObject<string>(response);
var projects = JsonConvert.SeseializeObject<List<ProjectResponse>>(json);
Upvotes: 1