Json序列化,键既是字符串又是对象

时间:2019-04-16 09:04:39

标签: c# json serializer

我正在努力进行JSON序列化,其中JSON blob有一个名为 answerValue 的键,它既是字符串又是对象,我真的不知道如何使用该键。 , 我试图创建一个名为AnswerValues的新类,问题是它同时返回AnswersValue和AnswerValue,并且正如您在blob中看到的那样,它只应返回其中之一。 关于如何构造类以使其正常工作的任何提示都应该?

  var jsonBody = new Root()


      {
            questions = new[]
               {
                   new Question() {
                   questionId = "3.01",
                   question ="Ipsum Lorem",
                       answers = new []
                        {
                            new Answer(){
                                answerChoiceId = "b2b-3.01",
                                    answerValue = new AnswerValue(){
                                    answerValue = "Yes"
                               }
                            }
                        }
                    },
                   new Question() {
                   questionId = "3.01.01",
                   question ="Ipsum Lorem",
                       answers = new []
                        {
                            new Answer(){
                             answerChoiceId = null,
                                answerValue = new AnswerValue()
                                 {
                                     type = "CountryAndTin",
                                     isoCode = "DK",
                                     tin = "123456"
                                 }
                            },
                            new Answer(){
                             answerChoiceId = null,
                                answerValue = new AnswerValue()
                                 {
                                     type = "CountryAndTin",
                                     isoCode = "NO",
                                     tin = "67467467"
                                 }
                            }
                        }
                    },
                    new Question() {
                   questionId = "3.02",
                   question ="Ipsum Lorem",
                       answers = new []
                        {
                            new Answer(){
                                   answerChoiceId = "b2b-3.02",
                                   answerValue = new AnswerValue(){
                                    answerValue = "Nej"
                               }
                            }
                        }
                    },
                       new Question() {
                   questionId = "3.03",
                   question ="Ipsum Lorem",
                       answers = new []
                        {
                            new Answer(){
                                answerChoiceId = "b2b-3.03",
                                answerValue = new AnswerValue(){
                                answerValue = "Nej"
                                }
                            }
                        }
                    }
                }
        };



     public partial class Root
        {
            [JsonProperty("questions")]
            public Question[] questions { get; set; }
        }

        public partial class Question
        {
            [JsonProperty("questionId")]
            public string questionId { get; set; }

            [JsonProperty("question")]
            public string question { get; set; }

            [JsonProperty("answers")]
            public Answer[] answers { get; set; }
        }

        public class Answer
        {
            [JsonProperty("answerChoiceId")]
            public string answerChoiceId { get; set; }


            [JsonProperty("answerValue")]
             public AnswerValue answerValue { get; set; }

            [JsonProperty("answerValue")]
            public AnswerValues answerValues { get; set; }

        }

        public class AnswerValue
        {
            [JsonProperty("answerValue")]
            public string answerValue { get; set; }
        }

        public class AnswerValues
        {
            [JsonProperty("type")]
            public string type { get; set; }

            [JsonProperty("isoCode")]
            public string isoCode { get; set; }

            [JsonProperty("tin")]
            public string tin { get; set; }
        }

0 个答案:

没有答案