PHP json_encode to C# Object unable to convert
Get the solution ↓↓↓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;
}
}
Answer
Solution:
Update:
Final solution was that there was ajson_encode
too much.
On the line beforevar 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);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: port 80 in use by "unable to open process" with pid 4!
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.