Search code examples
c#json.net

Deserializing a json that contains an xml declaration


I have the following json string:

{
  "?xml": {
    "@version": "1.0",
    "@encoding": "UTF-8"
  },
  "USER_DETAILS": {
    "USER": {
      "USR_ID": "452",
      "USR_FIRST_NAME": "First name",
      "USR_LAST_NAME": "Last name"
  }
}

I'm deserializing it using Newtonsoft.Json:

dynamic resultJson2 = JsonConvert.DeserializeObject(MyJsonString);    

How am I supposed to access my data now? I get something like this:

enter image description here

This doesn't work:

resultJson2[1].USER_LOGON.USER

Solution

  • Your JSON contains 2 properties - xml and USER_DETAILS (don't know why you have USER_LOGON in debug window, maybe some settings not shown in question).

    So you can access e.g. FIRST_NAME:

    var firstName = resultJson2.USER_DETAILS.USER.USR_FIRST_NAME; // or USER_LOGON instead of USER_DETAILS