【问题标题】:How to Serialize JSON that changes to a C# Class with RestSharp?如何使用 RestSharp 序列化更改为 C# 类的 JSON?
【发布时间】:2016-03-23 23:31:34
【问题描述】:

我这里有这个 JSON:

{
    "id": "-KDYmr0aI0UmjKRyd465",
    "name": "No Name",
    "presentation": {
      "fields": [
        {
          "defaultValue": "Erste Node",
          "id": "test",
          "title": "test",
          "type": "text"
        },
        {
          "defaultValue": "Zweite node",
          "id": "test2",
          "title": "test2",
          "type": "text"
        }
      ]
    },
    "thumbnail": "/images/defaultimage.png",
    "updated": "2016-03-23T14:05:32+00:00",
    "userData": {
      "test": "Erste Node",
      "test2": "Zweite node"
    }
  }

userData 的变化取决于用户是否添加了一些数据。所以假设用户添加了一些数据,例如{"test3":"More testdata"},userdata json 看起来像这样:

 "userData": {
      "test": "Erste Node",
      "test2": "Zweite node",
      "test3": "More testdata"
    }

字段也会相应更新,但我知道如何将这些字段映射到 C# 类。 我的问题是我如何能够将此 userData 序列化为 C# 类 - 而且还可以从这里使用 RestSharp 客户端:http://restsharp.org/

这几乎就是我的课程的样子。但我不知道如何映射 userData...

public class Field
{
    public string defaultValue { get; set; }
    public string id { get; set; }
    public string title { get; set; }
    public string type { get; set; }
}

public class Presentation
{
    public List<Field> fields { get; set; }
}

public class RootObject
{
    public string id { get; set; }
    public string name { get; set; }
    public Presentation presentation { get; set; }
    public string thumbnail { get; set; }
    public string updated { get; set; }
    public UserData userData { get; set; }
}

【问题讨论】:

    标签: c# json serialization restsharp


    【解决方案1】:

    这里有一个快速提示,对于用户数据,您应该使用 Dictionary。这将灵活地动态添加尽可能多的 userData(键值对)。重要的是,这将像普通类一样序列化为 JSON。

    【讨论】:

      【解决方案2】:

      您可以使用 JSON.NET 将 json 序列化为 c# 类,我相信您知道(我认为 RestSharp 使用这个库)。当您从服务中获取 json 时,使用 DeserializeObject 并定义一个要映射到的类:

      var json = "{ \"id\": \"-KDYmr0aI0UmjKRyd465\", \"name\": \"No Name\", \"presentation\": { \"fields\": [ { \"defaultValue\": \"Erste Node\", \"id\": \"test\", \"title\": \"test\", \"type\": \"text\" }, { \"defaultValue\": \"Zweite node\", \"id\": \"test2\", \"title\": \"test2\", \"type\": \"text\" } ] }, \"thumbnail\": \"/images/defaultimage.png\", \"updated\": \"2016-03-23T14:05:32+00:00\", \"userData\": { \"test\": \"Erste Node\", \"test2\": \"Zweite node\" } }";
      var result = JsonConvert.DeserializeObject<RootObject>(json);
      

      现在要处理可以更改为不同长度的 userData 节点,我会将我的类更改为可以处理此数据的“动态”类型。所以我的班级会变成:

      public class RootObject
      {
          public string id { get; set; }
          public string name { get; set; }
          public Presentation presentation { get; set; }
          public string thumbnail { get; set; }
          public string updated { get; set; }
          public dynamic userData { get; set; }
      }
      

      迭代您的动态 userData 属性:

      foreach(var data in result.userData)
      {
          var name = data.Name;
          var value = data.Value;
      }
      

      【讨论】:

      • 我从来没有使用过这样的动态类。我可以遍历这些或者我将如何在我的情况下使用它们?
      【解决方案3】:

      我认为,您可以将 Dictionary 用于 userData 属性

      public class Field
      {
          public string defaultValue { get; set; }
          public string id { get; set; }
          public string title { get; set; }
          public string type { get; set; }
      }
      
      public class Presentation
      {
          public List<Field> fields { get; set; }
      }
      
      public class RootObject
      {
          public string id { get; set; }
          public string name { get; set; }
          public Presentation presentation { get; set; }
          public string thumbnail { get; set; }
          public string updated { get; set; }
          public Dictionary<string, string> userData { get; set; }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-06-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-30
        • 1970-01-01
        • 1970-01-01
        • 2015-12-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多