1

I'm having a hard time deserializing a JSON to a concrete type, because I think the JSON i'm working with has been serialized incorrectly.

The JSON looks like this:

{
   "D16":"{\r\n  \"OptionsList\": [\r\n    {\r\n      \"Id\": 0,\r\n      \"Value\": \"Yes\",\r\n      \"IsHighlighted\": false,\r\n      \"Color\": \"\",\r\n      \"FieldsToProcess\": []\r\n    },\r\n    {\r\n      \"Id\": 1,\r\n      \"Value\": \"No\",\r\n      \"IsHighlighted\": false,\r\n      \"Color\": \"\",\r\n      \"FieldsToProcess\": []\r\n    }\r\n  ],\r\n  \"SingleSelectedOption\": null,\r\n  \"SelectedOptions\": [\r\n    {\r\n      \"Id\": 0,\r\n      \"Value\": \"Yes\",\r\n      \"IsHighlighted\": false,\r\n      \"Color\": \"\",\r\n      \"FieldsToProcess\": []\r\n    },\r\n    {\r\n      \"Id\": 1,\r\n      \"Value\": \"No\",\r\n      \"IsHighlighted\": false,\r\n      \"Color\": \"\",\r\n      \"FieldsToProcess\": []\r\n    }\r\n  ]\r\n}",
   "D5":"{\r\n  \"OptionsList\": [\r\n    {\r\n      \"Id\": 0,\r\n      \"Value\": \"Male\",\r\n      \"IsHighlighted\": false,\r\n      \"Color\": \"\",\r\n      \"FieldsToProcess\": []\r\n    },\r\n    {\r\n      \"Id\": 1,\r\n      \"Value\": \"Female\",\r\n      \"IsHighlighted\": false,\r\n      \"Color\": \"\",\r\n      \"FieldsToProcess\": []\r\n    }\r\n  ],\r\n  \"SingleSelectedOption\": {\r\n    \"Id\": 0,\r\n    \"Value\": \"Male\",\r\n    \"IsHighlighted\": false,\r\n    \"Color\": \"\",\r\n    \"FieldsToProcess\": []\r\n  },\r\n  \"SelectedOptions\": []\r\n}",
   "D15":"3213131",
   "D14":"321321",
   "D13":"213213",
   "D12":"213213",
   "D4":"Kerkata",
   "D3":"Kero",
   "D11":"1323",
   "D10":"1321",
   "D9":"321",
   "D8":"31321",
   "D7":"kero@gmail.com",
   "D1":"Kerim",
   "D2":"Emurla"
}

enter image description here

As you can see my problem is that the D16 object starts with quotes, which means I can't deserialize it to a concrete type.

I need to convert D16(I can have N number of those object) to a normal object in order to use JsonConvert.DeserializeObject<T> to convert it to the type I want.

Edit: This is what my class i'm trying to deserialize to looks like:

 public class FormDescriptionDTO
    {
        [JsonProperty(FormDescriptionsConstansts.FirstName)]
        public string FirstName { get; set; }

        [JsonProperty(FormDescriptionsConstansts.LastName)]
        public string LastName { get; set; }

        [JsonProperty(FormDescriptionsConstansts.MiddleName)]
        public string MiddleName { get; set; }

        [JsonProperty(FormDescriptionsConstansts.Preferred)]
        public string Preferred { get; set; }

        [JsonProperty(FormDescriptionsConstansts.Gender)]
        public OptionsDTO Gender { get; set; }

        [JsonProperty(FormDescriptionsConstansts.Email)]
        public string Email { get; set; }

        [JsonProperty(FormDescriptionsConstansts.SSN)]
        public string SSN { get; set; }

        [JsonProperty(FormDescriptionsConstansts.WorkPhone)]
        public string WorkPhone { get; set; }

        [JsonProperty(FormDescriptionsConstansts.CellPhone)]
        public string CellPhone { get; set; }

        [JsonProperty(FormDescriptionsConstansts.HomePhone)]
        public string HomePhone { get; set; }
    }

In this case the D16 should map to the Gender property.

Kerim Emurla
  • 1,141
  • 8
  • 15
  • 2
    The D16 object is just a string. Put it into a deserializer, and you can deserialize it. – Mafii Apr 23 '18 at 15:14
  • "I need to convert D16 to a normal object in order to use JsonConvert.DeserializeObject to convert it to the type I want." Why is that? You can just pass in a string to `DeserializeObject`. – pushkin Apr 23 '18 at 15:15
  • 2
    You didnt show anything related to how you are deserializing so we cant say if that is correct (and too many here *say* 'deserialize' when they are actually parsing). Show us the code – Ňɏssa Pøngjǣrdenlarp Apr 23 '18 at 15:16
  • `I think the JSON i'm working with has been serialized incorrectly.` Are you in control of the data source? is this an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? – Nkosi Apr 23 '18 at 15:18
  • @Plutonix, I've edited my question. – Kerim Emurla Apr 23 '18 at 15:21
  • 1
    Where did that class come from? None of those property names - short or longer form - appear in the JSOn at all. D16 isnt just Gender, but appears to include other types or arrays plus some other things – Ňɏssa Pøngjǣrdenlarp Apr 23 '18 at 15:29
  • You could apply `[JsonConverter(typeof(EmbeddedLiteralConverter))]` to `public OptionsDTO Gender`, where `EmbeddedLiteralConverter` comes from [How do I convert an escaped JSON string within a JSON object?](https://stackoverflow.com/q/39154043/3744182). And actually, this might be a duplicate. Agree? – dbc Apr 23 '18 at 18:24
  • Yes, I actually ended up creating a custom JsonConverter, which converts the string to OptionsDTO with some additional logic for checking if the string is an object or not and it's working fine now. Thank you all for your help. – Kerim Emurla Apr 23 '18 at 19:14

0 Answers0