Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用RestSharp序列化更改为C#类的JSON?_C#_Json_Serialization_Restsharp - Fatal编程技术网

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

如何使用RestSharp序列化更改为C#类的JSON?,c#,json,serialization,restsharp,C#,Json,Serialization,Restsharp,我这里有一个JSON: { "id": "-KDYmr0aI0UmjKRyd465", "name": "No Name", "presentation": { "fields": [ { "defaultValue": "Erste Node", "id": "test", "title": "test", "type": "text" },

我这里有一个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客户端:

这就是我的课程的大致情况。但我不知道如何映射用户数据

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; }
}
公共类字段
{
公共字符串defaultValue{get;set;}
公共字符串id{get;set;}
公共字符串标题{get;set;}
公共字符串类型{get;set;}
}
公开课演示
{
公共列表字段{get;set;}
}
公共类根对象
{
公共字符串id{get;set;}
公共字符串名称{get;set;}
公共演示文稿{get;set;}
公共字符串缩略图{get;set;}
公共字符串已更新{get;set;}
公共用户数据用户数据{get;set;}
}

您可以将json序列化为c#类,我相信您一定知道(我认为RestSharp使用这个库)。从服务获取json后,请使用反序列化对象并定义要映射到的类:

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属性,请执行以下操作:

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

您可以将json序列化为一个c#类,使用这个c#类,我相信您一定知道(我认为RestSharp使用这个库)。从服务获取json后,请使用反序列化对象并定义要映射到的类:

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属性,请执行以下操作:

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

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

这里有一个快速提示,对于用户数据,您应该使用字典。这将为动态添加尽可能多的用户数据(键值对)提供灵活性。重要的是,这将像普通类一样序列化为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; }
}
公共类字段
{
公共字符串defaultValue{get;set;}
公共字符串id{get;set;}
公共字符串标题{get;set;}
公共字符串类型{get;set;}
}
公开课演示
{
公共列表字段{get;set;}
}
公共类根对象
{
公共字符串id{get;set;}
公共字符串名称{get;set;}
公共演示文稿{get;set;}
公共字符串缩略图{get;set;}
公共字符串已更新{get;set;}
公共字典用户数据{get;set;}
}

我认为,您可以使用字典作为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; }
}
公共类字段
{
公共字符串defaultValue{get;set;}
公共字符串id{get;set;}
公共字符串标题{get;set;}
公共字符串类型{get;set;}
}
公开课演示
{
公共列表字段{get;set;}
}
公共类根对象
{
公共字符串id{get;set;}
公共字符串名称{get;set;}
公共演示文稿{get;set;}
公共字符串缩略图{get;set;}
公共字符串已更新{get;set;}
公共字典用户数据{get;set;}
}

我从未使用过这样的动态类。我可以迭代这些类吗?或者在我的例子中如何使用它们?我从来没有使用过这样的动态类。我可以重复这些吗?或者在我的案例中如何使用它们?