需要帮助序列化/反序列化这个 JSON

我有这个简单的 JSON,我正在反序列化这个 JSON,所以我可以获得元素。这是我拥有的 JSON:


{

  "QuestionIDs": [

    "QID1",

    "QID3"

  ],

  "QuestionDefinitions": {

    "Question1": {

      "DETag": "Q1",

      "Config": {

        "QDescription": "UseText"

      },

      "COrder": [

        "1",

        "2",

        "3"

      ],

      "Validation": {

        "Settings": {

          "ForceResponse": "OFF",

          "ForceResponseType": "ON",

          "Type": "None"

        }

      }

    }

  },

  "NextButton": null,

  "PreviousButton": false

}

这是我写的代码:


[DataContract]

public class RootObject

{

    [DataMember]

    public Dictionary<string, Dictionary<string, string>> QuestionDefinitions { get; set; }

    [DataMember]

    public List<string> QuestionIDs { get; set; }

}

QuestionIDs工作得很好。但是,QuestionDefinitions不起作用。它说这是一个空序列。我不确定出了什么问题。


我希望能够访问QuestionDefinitions.


我试过Json.Net。面临同样的问题。我能够得到简单的元素。但是,无法访问QuestionDefinitions.


任何帮助,将不胜感激。


萧十郎
浏览 152回答 3
3回答

慕标琳琳

您针对返回的 JSON 的类结构不正确。将您的 JSON 粘贴到json2csharp.com中时,会生成以下 POCO 类:public class Config{&nbsp; &nbsp; public string QDescription { get; set; }}public class Settings{&nbsp; &nbsp; public string ForceResponse { get; set; }&nbsp; &nbsp; public string ForceResponseType { get; set; }&nbsp; &nbsp; public string Type { get; set; }}public class Validation{&nbsp; &nbsp; public Settings Settings { get; set; }}public class Question1{&nbsp; &nbsp; public string DETag { get; set; }&nbsp; &nbsp; public Config Config { get; set; }&nbsp; &nbsp; public List<string> COrder { get; set; }&nbsp; &nbsp; public Validation Validation { get; set; }}public class QuestionDefinitions{&nbsp; &nbsp; public Question1 Question1 { get; set; }}public class RootObject{&nbsp; &nbsp; public List<string> QuestionIDs { get; set; }&nbsp; &nbsp; public QuestionDefinitions QuestionDefinitions { get; set; }&nbsp; &nbsp; public object NextButton { get; set; }&nbsp; &nbsp; public bool PreviousButton { get; set; }}现在使用上述类作为 JSON 结构的表示,您应该能够将其反序列化为 C# 对象。以下代码行将使用 NewtonSoft 库来完成:string json = "your json result";RootObject result = JsonConvert.DeserializeObject<RootObject>(json);如果您有 2013 或更高版本,您甚至可以在 Visual Studio 中执行此操作,如下文所述:

撒科打诨

也许您可以使用以下内容:public class Config{&nbsp; &nbsp; public string QDescription { get; set; }}public class Settings{&nbsp; &nbsp; public string ForceResponse { get; set; }&nbsp; &nbsp; public string ForceResponseType { get; set; }&nbsp; &nbsp; public string Type { get; set; }}public class Validation{&nbsp; &nbsp; public Settings Settings { get; set; }}public class Question{&nbsp; &nbsp; public string DETag { get; set; }&nbsp; &nbsp; public Config Config { get; set; }&nbsp; &nbsp; public List<string> COrder { get; set; }&nbsp; &nbsp; public Validation Validation { get; set; }}public class RootObject{&nbsp; &nbsp; public List<string> QuestionIDs { get; set; }&nbsp; &nbsp; public Dictionary<string, Question> QuestionDefinitions { get; set; }&nbsp; &nbsp; public object NextButton { get; set; }&nbsp; &nbsp; public bool PreviousButton { get; set; }}

凤凰求蛊

试试这个模型public class RootObject{&nbsp; &nbsp; public List<string> QuestionIDs { get; set; }&nbsp; &nbsp; public QuestionDefinitions QuestionDefinitions { get; set; }&nbsp; &nbsp; public object NextButton { get; set; }&nbsp; &nbsp; public bool PreviousButton { get; set; }}public class QuestionDefinitions{&nbsp; &nbsp; public Question1 Question1 { get; set; }&nbsp; &nbsp; public List<string> COrder { get; set; }&nbsp; &nbsp; public Validation Validation { get; set; }}public class Question1 {&nbsp; &nbsp; public string DETag { get; set; }&nbsp; &nbsp; public Config Config { get; set; }}public class Config {&nbsp; &nbsp; public string QDescription { get; set; }}public class Validation {&nbsp; &nbsp; public Settings Settings { get; set; }}public class Settings{&nbsp; &nbsp; public string ForceResponse { get; set; }&nbsp; &nbsp; public string ForceResponseType { get; set; }&nbsp; &nbsp; public string Type { get; set; }}
打开App,查看更多内容
随时随地看视频慕课网APP