Reputation: 3024
I have this array of objects that contains some strings and an array of strings that I want to return from my server:
"[{"original_file_name":"Plain_HAIR.pdf","encrypted_file_name":"UTTSJG13V1EC46J.pdf","is_successful":true,"message":"","email":"[\"[email protected]\"]","doctype":"HAIR"}]"
On my local app, I have a class that looks like this:
public class FileUploadResultViewModel
{
public string original_file_name { get; set; }
public string encrypted_file_name { get; set; }
public string is_successful { get; set; }
public string message { get; set; }
public string email { get; set; }
public string doctype { get; set; }
}
if (res.StatusCode == System.Net.HttpStatusCode.OK)
{
return JsonConvert.DeserializeObject<List<FileUploadResultViewModel>>(res.Content);
}
else
{
return null;
}
The issue is that when it comes back to the local app, I get an error saying :
{"Error converting value "[{"original_file_name":"Plain_HAIR.pdf","encrypted_file_name":"UTTSJG13V1EC46J.pdf","is_successful":true,"message":"","email":"["[email protected]"]","doctype":"HAIR"}]" to type 'System.Collections.Generic.List`1[Uploader.ViewModels.FileUploadResultViewModel]'. Path '', line 1, position 1521."}
Am I converting it wrongly or are there some steps missing?
EDIT:
This is how I define the object on the server:
$object = (object) [
'original_file_name' => $item["original_file_name"],
'encrypted_file_name' => $item["file_name"],
'is_successful' => true,
'message' => '',
'email' => NULL,
'doctype' => $item["doctype"]
];
array_push($myArray, $object);
$this->response(json_encode($myArray));
EDIT 2: Showing the method to retrieve the json result
public FileUploadResultViewModel Upload(List<FileUploadViewModel> sdvm)
{
var js = JsonConvert.SerializeObject(sdvm);
RestClient client = new RestClient();
var link = url;
var username = username;
var password = password;
client.BaseUrl = new Uri(link);
client.Authenticator = new HttpBasicAuthenticator(username, password);
RestRequest request = new RestRequest();
request.AddJsonBody(sdvm);
request.Method = Method.POST;
var res = client.Execute(request);
if (res.StatusCode == System.Net.HttpStatusCode.OK)
{
return JsonConvert.DeserializeObject<FileUploadResultViewModel>(res.Content);
}
else
{
return null;
}
}
Upvotes: 2
Views: 187
Reputation: 11124
Update:
Final solution was that there was a json_encode
too much.
On the line before var res = client.Execute(request);
, add this:
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
This is working:
var json = @"[{""original_file_name"":""Plain_HAIR.pdf"",""encrypted_file_name"":""UTTSJG13V1EC46J.pdf"",""is_successful"":true,""message"":"""",""email"":""[\""[email protected]\""]"",""doctype"":""HAIR""}]";
var convertedInstance = JsonConvert.DeserializeObject<List<FileUploadResultViewModel>>(json);
And surrounding with "
is also working
var json2 = @"""[{""original_file_name"":""Plain_HAIR.pdf"",""encrypted_file_name"":""UTTSJG13V1EC46J.pdf"",""is_successful"":true,""message"":"""",""email"":""[\""[email protected]\""]"",""doctype"":""HAIR""}]""";
var convertedInstance2 = JsonConvert.DeserializeObject<List<FileUploadResultViewModel>>(json);
Upvotes: 1