如何使用 RestSharp 序列化更改为 C# Class 的 JSON?

How to Serialize JSON that changes to a C# Class with RestSharp?

我这里有 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"},用户数据 json 将如下所示:

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

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

这就是我的 类 的样子。但是我不知道如何映射用户数据...

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

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

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 节点,我会将我的 class 更改为可以处理此数据的 'dynamic' 类型。所以我的 class 会变成:

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

迭代你的动态用户数据属性:

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

这里的一个快速提示是,对于用户数据,您应该使用字典。这将提供动态添加尽可能多的用户数据(键值对)的灵活性。重要的是,这将像正常的 class.

一样序列化为 JSON

我想,你可以使用 Dictionary for 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; }
}