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

标签 c# json json.net

我有这个简单的 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

如有任何帮助,我们将不胜感激。

编辑:

我也试过像这样实现这个类:

[DataContract]
public class QuestionDetails
{
    [DataMember]
    public string DETag { get; set; }
    [DataMember]
    [DataMember]
    public List<Configuration> Config { get; set; }
    [DataMember]
    public List<string> ChoiceOrder { get; set; }
    [DataMember]
    public List<Validation> Validation { get; set; }
    [DataMember]
    public List<string> COrder { get; set; }
    [DataMember]
    public string NextButton { get; set; }
    [DataMember]
    public string PreviousButton { get; set; }
}


    [DataContract]
    public class RootObject
    {
        [DataMember]
        public Dictionary<string, QuestionDetails> QuestionDefinitions { get; set; }
        [DataMember]
        public List<string> QuestionIDs { get; set; }
    }

最佳答案

针对返回的 JSON 的类结构不正确。

当将您的 JSON 粘贴到 json2csharp.com 时,生成以下 POCO 类:

public class Config
{
    public string QDescription { get; set; }
}

public class Settings
{
    public string ForceResponse { get; set; }
    public string ForceResponseType { get; set; }
    public string Type { get; set; }
}

public class Validation
{
    public Settings Settings { get; set; }
}

public class Question1
{
    public string DETag { get; set; }
    public Config Config { get; set; }
    public List<string> COrder { get; set; }
    public Validation Validation { get; set; }
}

public class QuestionDefinitions
{
    public Question1 Question1 { get; set; }
}

public class RootObject
{
    public List<string> QuestionIDs { get; set; }
    public QuestionDefinitions QuestionDefinitions { get; set; }
    public object NextButton { get; set; }
    public bool PreviousButton { get; set; }
}

现在使用上述类作为 JSON 结构的表示,您应该能够将其反序列化为 C# 对象。

以下代码行将使用 NewtonSoft 库完成:

string json = "your json result";
RootObject result = JsonConvert.DeserializeObject<RootObject>(json);

如果您有 2013 或更高版本,您甚至可以在 Visual Studio 中执行此操作,如 this post 中所述:

enter image description here

希望对您有所帮助。

关于c# - 需要帮助序列化/反序列化这个 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51765253/

相关文章:

c# - 你能喝一个 boost::optional<> 吗?

c# - LINQ 投影上的代码覆盖率

arrays - PLpgSQL:将每条记录存储在For循环中并返回json

json - 使用 jq 附加 JSON 对象

javascript - Jquery 自动 json 命名

c# - 使用 json.net/webapi 在 C# 中反序列化只获取属性

c# - 跨多个 Rx 主题的调用订单是否可以保证订单?

c# - 使用 BouncyCaSTLe 验证 ECDSA 签名

c# - 反序列化 Json 以列出 C#

c# - 循环遍历 JObject c#