Desrializing jsonresult throws error in razor pages.

Blooming Developer 196 Reputation points
2023-01-17T03:57:20.3933333+00:00

Hi,

I have a json result like shown below,

"{\r\n  \"AccessRights\": [],\r\n  \
"ADAttributes\": [
\r\n    {\r\n      \
"AD\": \"teenajohn\",\r\n      \
"ADFull\": \"\",\r\n      \
"DisplayName\": \"Teena John\",\r\n      \
"FirstName\": \"Teena\",\r\n      \
"Initials\": \"\",\r\n      \
"EmployeeNumber\": \"1111111\",\r\n      \
"LastName\": \"John\",\r\n      \
"EmailAddress\": \"Teena.John@gmail.com\",\r\n      \
"Department\": \"Information Systems\",\r\n      \
"Title\": \"System Analyst\",\r\n      \
"BusinessPhone2\": \"6666111111\",\r\n      \
"Mobile\": \"\",\r\n      \
"CleanMobile\": \"\",\r\n      \
"Okta\": \"Teena.John@gmail.com\"\r\n    }\r\n  
],
\r\n  \
"OktaAttributes\": 
[\r\n    {\r\n      \
"Okta\": \"Teena.John@gmail.com\",\r\n      \
"AD\": \"teenajohn\",\r\n      \
"EmployeeNumber\": \"1111111\",\r\n      \
"FirstName\": \"Teena\",\r\n      \
"LastName\": \"John\",\r\n      \
"Email\": \"Teena.John@gmail.com\",\r\n      \
"SecondEmail\": \"\",\r\n      \
"ManagerAD\": \"\",\r\n      \
"ManagerEmail\": \"\",\r\n      \
"HRAdvisor\": \"\",\r\n      \
"Title\": \"System Analyst\",\r\n      \
"RockFunction\": \"\",\r\n      \
"Mobile\": \"\",\r\n      }\r\n  
],\r\n  \
"IsPublic\": false,\r\n  \
"AuthenticateType\": 1\r\n
}"

To reterive the ADAttributes like AD, DisplayName etc i created a Model Class like this

 public class AuthenticateUserInfo
    {
        public Dictionary<string, ADUserInfo>? ADAttributes { get; set; }
        public Dictionary<string, OktaUserInfo>? OktaAttributes { get; set; }

    }

    public class ADUserInfo
    {
        public string AD { get; set; }
        public string ADFull { get; set; }      
        public string DisplayName { get; set; }
        public string FirstName { get; set; }
        public string Initials { get; set; }
        public string EmployeeNumber { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
        public string Department { get; set; }
        public string Title { get; set; }
        public string Manager { get; set; }
    }

    public class OktaUserInfo
    {

    }

And used json deserialize in my .cshtml.cs file to retrieve those details.

But system throws error

 if (result.Success)
                {
                    Console.WriteLine("Success");
                    string jsonString = result.JsonResult.ToString();
                    AuthenticateUserInfo AuthenticateUserInfo = JsonSerializer.Deserialize<AuthenticateUserInfo>(jsonString);
               
                    return Redirect("/Privacy");

                }

could not figure out where i am going wrong.

Any help would be appreciated.Thanks!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
2,588 questions
No comments
{count} votes

Accepted answer
  1. Xinran Shen - MSFT 1,041 Reputation points Microsoft Employee
    2023-01-17T07:28:34.1833333+00:00

    Hi @Blooming Developer ,

    From your description of this question, I think you want to desrialize json to objects. So you can check this simple demo.

    Let's remove escape characters from this json:

    {
        "AccessRights": [],
        "ADAttributes": [
            {
                "AD": "teenajohn",
                "ADFull": "",
                "DisplayName": "Teena John",
                "FirstName": "Teena",
                "Initials": "",
                "EmployeeNumber": "1111111",
                "LastName": "John",
                "EmailAddress": "Teena.John@gmail.com",
                "Department": "Information Systems",
                "Title": "System Analyst",
                "BusinessPhone2": "6666111111",
                "Mobile": "",
                "CleanMobile": "",
                "Okta": "Teena.John@gmail.com"
            }
        ],
        "OktaAttributes": [
            {
                "Okta": "Teena.John@gmail.com",
                "AD": "teenajohn",
                "EmployeeNumber": "1111111",
                "FirstName": "Teena",
                "LastName": "John",
                "Email": "Teena.John@gmail.com",
                "SecondEmail": "",
                "ManagerAD": "",
                "ManagerEmail": "",
                "HRAdvisor": "",
                "Title": "System Analyst",
                "RockFunction": "",
                "Mobile": "",
                
            }
        ],
        "IsPublic": false,
        "AuthenticateType": 1
    }
    
    

    There are five properties in this json: AccessRights,ADAttributes,OktaAttributes,IsPublic,AuthenticateType.

    So you can create a class contains these properties.

    AuthenticateUserInfo

    public class AuthenticateUserInfo
        {
            public List<AccessRights> accessRights { get; set; }
            public List<ADAttributes> adAttributes { get; set; }
            public List<OktaAttributes> oktaAttributes { get; set; }
            public bool IsPublic { get; set; }
            public int AuthenticateType { get; set; }
        }
    

    ADAttributes

    public class ADAttributes
        {
            public string AD { get; set; }
            public string ADFull { get; set; }
            public string DisplayName { get; set; }
            public string FirstName { get; set; }
            public string Initials { get; set; }
            public string EmployeeNumber { get; set; }
            public string LastName { get; set; }
            public string EmailAddress { get; set; }
            public string Department { get; set; }
            public string Title { get; set; }
            public string Manager { get; set; }
    
            public string BusinessPhone2 { get; set; }
    
            public string Mobile { get; set; }
    
            public string CleanMobile { get; set; }
    
            public string Okta { get; set; }
        }
    

    OktaAttributes

     public class OktaAttributes
    {
        public string Okta { get; set; }
        public string AD { get; set; }
        public string EmployeeNumber { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string SecondEmail { get; set; }
        public string ManagerAD { get; set; }
        public string ManagerEmail { get; set; }
        public string HRAdvisor { get; set; }
        public string Title { get; set; }
        public string RockFunction { get; set; }
        public string Mobile { get; set; }
    }
    

    Then you can use:

    AuthenticateUserInfo AuthenticateUserInfo = JsonConvert.DeserializeObject<AuthenticateUserInfo>(content);
    

    to get the value.

    DEMO:

    enter image description here

    enter image description here


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Xinran Shen


0 additional answers

Sort by: Most helpful