反序列化已经序列化两次的对象 - Newtonsoft JSON.NET

时间:2018-04-23 15:12:00

标签: c# json json.net

我很难将JSON反序列化为具体类型,因为我认为我正在使用的JSON序列化不正确。

JSON看起来像这样:

{
   "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

正如您所看到的,我的问题是D16对象以引号开头,这意味着我无法将其反序列化为具体类型。

我需要将D16(我可以将N个这样的对象)转换为普通对象,以便使用JsonConvert.DeserializeObject<T>将其转换为我想要的类型。

编辑:这是我的课程,我试图反序列化为:

 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; }
    }

在这种情况下,D16应映射到Gender属性。

0 个答案:

没有答案