Recover objects from response.Content.ReadAsStringAsync()

2.7k Views Asked by At

I have a list of Json objectS that I send by API as a result :

    {
  "Pr": {
    "ClientProfil": {
      "IdClient": 67,
      "FirmName": null,
      "NumMember": "OPTOO111",
      "FirstName": "EL MONAGI",
      "LastName": "ABDELJALIL",
      "Email": "jalil_monagi@yahoofr",
      "Adress": "7795 10eme avenue",
      "City": "Montréal",
      "Province": "QC",
      "Country": "CA",
      "Postalcode": "H2A3B3",
      "Tel": "438 995 6475",
      "PreferredLanguage": "fr"
    },
    "Programm": {
      "IdProgramm": 9,
      "Designation": "OPTO",
      "CreationDate": "2020-01-01T00:00:00",
      "SuppressionDate": null,
      "RenewDate": "2021-01-04T00:00:00",
      "StartDate": null,
      "IsRenewDate": true
    }
}

and in the front office I use the code below

public async Task<ActionResult> Authentificate(FormCollection form)
    {
        HttpClient client = autentificate();

        string userCodePwd = form["fCode"].ToString() + ":" + form["lPwd"].ToString();
        userCodePwd = Convert.ToBase64String(Encoding.Default.GetBytes(userCodePwd));

        //var decodedAuthenticationToken = Encoding.UTF8.GetString(Convert.FromBase64String(userCodePwd));
        
        HttpResponseMessage response = client.GetAsync("api/Login/FindPolice?userCodePasswordArray=" + userCodePwd).Result;

        try
        {
            if (response.IsSuccessStatusCode)
            {
                string Result = await response.Content.ReadAsStringAsync();

                JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
                dynamic dobj = jsonSerializer.Deserialize<dynamic>(Result);

                int StatusCode = (int)response.StatusCode;
                if (StatusCode==200) //  Existe une assurance pour le client
                {
                    int StatusRequest = dobj["StatusRequest"];
                    if (StatusRequest==1001)
                    {
                        int IdProgram = dobj["IdProgram"];
                        RootAQII.Profil= JsonConvert.DeserializeObject<Profil>(dobj["Pr"].ToString());

                        RootAQII.Programm= JsonConvert.DeserializeObject<List<Programm>>(dobj["Programm"].ToString());
                        
                                                    if (IdProgram==9)
                        {
                            return RedirectToAction("Index", "OPTO");
                        }
                    }
                }
            }
            else
            {

            }
        }
        catch (Exception e)
        {
            String message = e.Message;
        }

        return View();
    }

the problem is when I try to parse the result of response for RootOPTOASSURED.Profil

I get this message error :

unexpected character encountered while parsing value: s. path '', line 0, position 0.

there is anyone who has any idea And thanks you in advance :)

1

There are 1 best solutions below

0
On

Assuming that Result is the json string from above, the line

int IdProgram = dobj["IdProgram"];

Should fail as the only element of dobj should be dobj["PR"]

Besides that, you are trying to deserialize a list of Program entries from a single JSON entry. If Program is supposed to be a list, then it should appear in []

{
    "Pr": {
        "ClientProfil": {
            "IdClient": 67,
            "FirmName": null,
            "NumMember": "OPTOO111",
            "FirstName": "EL MONAGI",
            "LastName": "ABDELJALIL",
            "Email": "jalil_monagi@yahoofr",
            "Adress": "7795 10eme avenue",
            "City": "Montréal",
            "Province": "QC",
            "Country": "CA",
            "Postalcode": "H2A3B3",
            "Tel": "438 995 6475",
            "PreferredLanguage": "fr"
        },
        "Programm": 
        [
            {
                "IdProgramm": 9,
                "Designation": "OPTO",
                "CreationDate": "2020-01-01T00:00:00",
                "SuppressionDate": null,
                "RenewDate": "2021-01-04T00:00:00",
                "StartDate": null,
                "IsRenewDate": true
            }
        ]
    }
}

Either that or change the line

RootAQII.Programm= JsonConvert.DeserializeObject<List<Programm>>(dobj["Programm"].ToString());
                    

to

RootAQII.Programm= JsonConvert.DeserializeObject<Programm>(dobj["Programm"].ToString());
                    

I have put an example at rextester