2

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;
    }
}
6
  • 1
    I see one error: "is_successful":true is a bool, not a string Commented Jul 10, 2021 at 12:56
  • This doesn't really have anything to do with PHP. The function json_encode() just creates standard stringified json, so the fact that it comes from PHP is irrelevant here. Commented Jul 10, 2021 at 13:17
  • @MagnusEriksson I understand. I was just wondering if there was any issue with the json produced since c# can't seem to parse it to the object. Commented Jul 10, 2021 at 13:18
  • You need to define the correct data types, as @RoarS. pointed out. Commented Jul 10, 2021 at 13:19
  • @MagnusEriksson I just did a test by setting is_successful to bool and the same error occurred. Commented Jul 10, 2021 at 13:22

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

8 Comments

It works when testing with Postman for me too. It doesn't work when it comes into my local application.
@JianYA: How do you send this in JSON: 'email' => NULL ? Test JSON doesn't seem to reflect this.
This was from another test. I have also tried removing all fields and leaving is_successful and the same error occurs. It seems like my local app has difficulty deserializing the json from the server.
So far the only thing that has been successful is returning a simple string
I found out what was going on. $this->response already does json_encode! I have been doing double the work for no reason. My apologies.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.