Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to parse a Json string and fetch the code in the class ErrorDetail1 from the below. I am using asp.net with c#. I am getting null value
Code tried is as below:
var jsonData = System.IO.File.ReadAllText(@"E:\Alex\test.json");
var myDetails = Newtonsoft.Json.JsonConvert.DeserializeObject<ErrorDetail1>(jsonData);
string code= myDetails.Code;


What I have tried:

My json string is as below:
{
"Header": {
"Audit": {
"T24_time": 285,
"ResponseParse_time": 0,
"RequestParse_time": 167
},
"Id": "PI2206601ZB01YQ8",
"Status": "failed",
"UniqueIdentifier": "IRFX221580544240744.00"
},
"Error": {
"Type": "BUSINESS",
"ErrorDetails": [
{
"FieldName": "debitAccountId",
"Code": "E-117691",
"Message": "ACCOUNT RECORD MISSING"
}
]
},
"Override": {},
"TraceId": "1de9e971-8f5f-47d4-e2c3-0a45c9aa1dd8"
}


My class for above json string is as below:
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
    public class DDS_TRANSACT_POSTING_RESPONSE_FAILURE_DETAILS
    {
        public int T24_time { get; set; }
        public int ResponseParse_time { get; set; }
        public int RequestParse_time { get; set; }
    }

    public class Error
    {
        public string Type { get; set; }
        public List<ErrorDetail> ErrorDetails { get; set; }
    }

    public class ErrorDetail1
    {
        public string FieldName { get; set; }
        public string Code { get; set; }
        public string Message { get; set; }
    }

    public class Header
    {
        public DDS_TRANSACT_POSTING_RESPONSE_FAILURE_DETAILS Audit { get; set; }
        public string Id { get; set; }
        public string Status { get; set; }
        public string UniqueIdentifier { get; set; }
    }

    public class Override
    {
    }

    public class Root
    {
        public Header Header { get; set; }
        public Error Error { get; set; }
        public Override Override { get; set; }
        public string TraceId { get; set; }
    }
Posted
Updated 7-Oct-22 2:53am
v2

1 solution

The whole JSON is not an instance of an ErrorDetail (or ErrorDetail1): it's an instance of a Root. If you explicitly specify the class to deserialize to and it doesn't match the data, it will return an empty object.

Remove the specific class, and it'll start to work:
C#
var myDetails = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonData);
 
Share this answer
 
Comments
Member 7513082 7-Oct-22 9:15am    
How to fetch the value of Code coming under Error Detail in the response
OriginalGriff 7-Oct-22 10:10am    
Follow the property chain, just like you would with any other classes.
Member 7513082 7-Oct-22 10:32am    
Tried as below but didn't work
Error objResponseError = new Error();
ResponseErrorCode = objResponseError.ErrorDetails[0].Code;
ResponseErrorCodeDescription = objResponseError.ErrorDetails[0].Message;

Getting object refernece issue



json string getting as response is as below:
{
"Header": {
"Audit": {
"T24_time": 285,
"ResponseParse_time": 0,
"RequestParse_time": 167
},
"Id": "PI2206601ZB01YQ8",
"Status": "failed",
"UniqueIdentifier": "IRFX221580544240744.00"
},
"Error": {
"Type": "BUSINESS",
"ErrorDetails": [
{
"FieldName": "debitAccountId",
"Code": "E-117691",
"Message": "ACCOUNT RECORD MISSING"
}
]
},
"Override": {},
"TraceId": "1de9e971-8f5f-47d4-e2c3-0a45c9aa1dd8"
}
OriginalGriff 7-Oct-22 10:43am    
So ... you didn't read a single word I said, then.
Richard MacCutchan 7-Oct-22 10:48am    
You have mistyped the name of one of your classes. You have public class ErrorDetail1 instead of public class ErrorDetail.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900