0

Trying to get the value of a Json but keep throwing me this error.

enter image description here

This is the code:

string djson = JsonConvert.SerializeObject(e);


            var httpContent = new StringContent(djson, Encoding.UTF8, "application/json");

            var response = await client.PostAsync(Baseurl + "ex.api/api/exmaple/indi/getindicertification", httpContent);
            var jsonString = await response.Content.ReadAsStringAsync();
            var messageResponse = JsonConvert.DeserializeObject<Messages>(jsonString);
            var responseData = JsonConvert.DeserializeObject<ResultingRequest>(jsonString);

I also try this way too but keep trowing me a error too:

var responseData = JsonConvert.DeserializeObject<List<ResultingRequest>>(jsonString);

My model class looks like this:

public class ResultingRequest : EvaluationResult
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public DateTime RequestDate { get; set; }
    public int CertificationRequestStatus { get; set; }
    public int Language { get; set; }
    public string OGPCorrelationID { get; set; }
    public DateTime EmissionDate { get; set; }
    public object Reason { get; set; }
    public object Owner { get; set; }
    public string OGPATGNumber { get; set; }
    public object SolicitantName { get; set; }
    public object SolicitantIDNumber { get; set; }
    public DateTime ExpirationDate { get; set; }
    public bool CommitedToPRACSES { get; set; }
    public object Resolution { get; set; }
    public object Kiosco { get; set; }
    public object Comments { get; set; }
    public bool PorAcuerdo { get; set; }
    public int KioscoId { get; set; }
    public string EvaluationResult { get; set; }
}

And this is the Json:

{
"ResultingRequest": {
    "Id": 123 ,
    "UserId": 3456 ,
    "RequestDate": "09-20-2019",
    "CertificationRequestStatus": 4,
    "Language": 1,
    "OGPCorrelationID": "",
    "EmissionDate": "",
    "Reason": null,
    "Owner": null,
    "OGPATGNumber": "",
    "SolicitantName": null,
    "SolicitantIDNumber": null,
    "ExpirationDate": "",
    "CommitedToPRACSES": false,
    "Resolution": null,
    "Kiosco": null,
    "Comments": null,
    "PorAcuerdo": false,
    "KioscoId": 1,
    "EvaluationResult": "Negative"
},
"EvaluationResult": {
    "SolicitantIsMinor": false,
    "EvaluationResults": [],
    "IndividualHasNoCases": true,
    "IndividualDeceased": false,
    "CertificationType": "Negative",
    "RestrictedIndividual": false
},
"CertificationPDFBytes": "",
"SentByEmail": true,
"SentByEmailSuccess": true,
"Message": null,
"CertificationKey": ""
}

I want to get the value "EvaluationResult" from the ResultingRequest:{}. What do I do wrong or what I'm missing? I try a couple of ways I search for this but all the ways I keep receiving an error.

3
  • "Id": "", "UserId": "", Commented Mar 1, 2019 at 15:14
  • RequestDate will fail also. An empty string is not a valid date. Commented Mar 1, 2019 at 15:16
  • @Ralf I just erase the value for confidential propose. I just post the Json just for the view of the structure. Commented Mar 1, 2019 at 15:19

2 Answers 2

2

First of all if you Deserializing into a List your json request should be an array of ResultingRequest which right now he isn't because its an json object you have to add the [] to your json request. Example:

[{
"ResultingRequest": {
    "Id": "",
    "UserId": "",
    "RequestDate": "",
    "CertificationRequestStatus": 4,
    "Language": 1,
    "OGPCorrelationID": "",
    "EmissionDate": "",
    "Reason": null,
    "Owner": null,
    "OGPATGNumber": "",
    "SolicitantName": null,
    "SolicitantIDNumber": null,
    "ExpirationDate": "",
    "CommitedToPRACSES": false,
    "Resolution": null,
    "Kiosco": null,
    "Comments": null,
    "PorAcuerdo": false,
    "KioscoId": 1,
    "EvaluationResult": "Negative"
}}]

Secondly the rest the json will not be parsed since they aren't a ResultingRequest object, and finally like they mention here you weren't specifying some property values

Edit: Since you are saying that the request is being sent by an API and you don't wanna change the json format what you can do the following , parse the JSON into a JObject ( since your json it's an object and not an array ) then select which object you want to retrieve ( in your case its the ResultingRequest ) then deserialize it like so :

     var parsedObject = JObject.Parse(jsonString);
     var resultingRequestJson= parsedObject["ResultingRequest"].ToString();
     var responseData = JsonConvert.DeserializeObject<ResultingRequest>(resultingRequestJson);

I made a simple example of your json file and got some other further errors and to fix it I changed the Datetime properties into nullable because your json file its returning empty strings example:

public DateTime? EmissionDate { get; set; }
Sign up to request clarification or add additional context in comments.

5 Comments

The json is come like that without [] just with {}. New at this sorry.
It's fine just let me know where you struggling after changing the json to have an array or simply deserialize a ResultingRequest instead of list
I generate the Json with an API then I want to Deserialize to grab the "EvaluationResult" form "ResultingRequest": {. Like this var messageResponse = JsonConvert.DeserializeObject<Messages>(jsonString); is work fine.
change to var responseData = JsonConvert.DeserializeObject<ResultingRequest>(jsonString); since you are not returning an array of it
Yas!! finally thank you so much for that extraordinary explanation and step by step!! is working perfectly fine!!!
0

Your Json is wrong, note that the first attribute in your Json (Id and UserId are missing quotes) Try to use it this way:

[{
  "ResultingRequest": {
    "Id": "",
    "UserId": "",
    "RequestDate": "",
    "CertificationRequestStatus": 4,
    "Language": 1,
    "OGPCorrelationID": "",
    "EmissionDate": "",
    "Reason": null,
    "Owner": null,
    "OGPATGNumber": "",
    "SolicitantName": null,
    "SolicitantIDNumber": null,
    "ExpirationDate": "",
    "CommitedToPRACSES": false,
    "Resolution": null,
    "Kiosco": null,
    "Comments": null,
    "PorAcuerdo": false,
    "KioscoId": 1,
    "EvaluationResult": "Negative"
  },
  "EvaluationResult": {
    "SolicitantIsMinor": false,
    "EvaluationResults": [],
    "IndividualHasNoCases": true,
    "IndividualDeceased": false,
    "CertificationType": "Negative",
    "RestrictedIndividual": false
  },
  "CertificationPDFBytes": "",
  "SentByEmail": true,
  "SentByEmailSuccess": true,
  "Message": null,
  "CertificationKey": ""
}]

6 Comments

The value is a int that's why come with out "".
Then you need to add a value, you can leave em empty. Add 0 if you want
That Json comes with all values but I erase for confidential propose I just add it like that to see the structure of the Json.
Ok then I suppose after getting the values and setting em, no problems have to occur when deserializing the object. Just simply you can't put empty (non-typed) elements inside of a json string, just like that: {"Id":}. And for further working use jsoneditoronline.org if you want to check a Json if is correct or not
The Json I receive is from an API then I want to Deserialize to grab a value from the Json.
|

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.