C#Newtonsoft.Json转换类型

C#Newtonsoft.Json转换类型,c#,json,types,C#,Json,Types,我必须: 反序列化对象-进行一些更改-序列化对象 当我的Json有多个类型时,例如 { "type": "test", "currentStatus": "Active", "id": "987", "items": [ { "type": "test1", "id": "123", "name": "Segment Members", "memberCount": "0", "outputTerminals":

我必须: 反序列化对象-进行一些更改-序列化对象 当我的Json有多个类型时,例如

{
  "type": "test",
  "currentStatus": "Active",
  "id": "987",
  "items": [
    {
      "type": "test1",
      "id": "123",
      "name": "Segment Members",
      "memberCount": "0",
      "outputTerminals": [
        {
          "type": "test2",
          "id": "123",
          "connectedId": "123",
          "terminalType": "out"
        }
      ],
      "position": {
        "type": "Position",
        "x": "46",
        "y": "14"
      },
      "isFinished": "true",
      "isRecurring": "false",
      "segmentId": "123"
    },
    {
      "type": "test5",
      "id": "1390",
      "name": "Yay! Clickers",
      "memberCount": "2",
      "position": {
        "type": "Position",
        "x": "330",
        "y": "375"
      },
      "waitFor": "2592000"
    },
    {
      "type": "test3",
      "id": "1391",
      "name": "test",
      "memberCount": "73",
      "outputTerminals": [
        {
          "type": "test4",
          "id": "123",
          "connectedId": "123",
          "connectedType": "CampaignWaitAction",
          "terminalType": "yes"
        },
        {
          "type": "test4",
          "id": "123",
          "connectedId": "123",
          "connectedType": "CampaignWaitAction",
          "terminalType": "no"
        }
      ],
      "position": {
        "type": "Position",
        "x": "123",
        "y": "123"
      },
      "testId": "123"
    }
  ]
}

此操作应使用什么数据类型?动态、对象、作业对象。。。?或者其他什么?

我只需要创建一个对象并将其反序列化为该类型。这比动态操作容易得多。(假设它将始终保持在该结构中)

该做什么

复制您的Json:

 `Edit` -> `Paste Special` -> `Paste JSON As CLASSES` 
就这样!您具有要反序列化到的类型

var deserializedJson = JsonConvert.DeserializeObject<YourNewObject>(jsonString);
var deserializedJson=JsonConvert.DeserializeObject(jsonString);

注意:如果粘贴json类无效,请确保您的json有效:

我只会创建一个对象并将其反序列化为该类型。这比动态操作容易得多。(假设它将始终保持在该结构中)

该做什么

复制您的Json:

 `Edit` -> `Paste Special` -> `Paste JSON As CLASSES` 
就这样!您具有要反序列化到的类型

var deserializedJson = JsonConvert.DeserializeObject<YourNewObject>(jsonString);
var deserializedJson=JsonConvert.DeserializeObject(jsonString);

注意:如果粘贴json类无效,请确保您的json有效:

您可以创建类并序列化/反序列化json:

 public class OutputTerminal
    {
        public string type { get; set; }
        public string id { get; set; }
        public string connectedId { get; set; }
        public string terminalType { get; set; }
        public string connectedType { get; set; }
    }

    public class Position
    {
        public string type { get; set; }
        public string x { get; set; }
        public string y { get; set; }
    }

    public class Item
    {
        public string type { get; set; }
        public string id { get; set; }
        public string name { get; set; }
        public string memberCount { get; set; }
        public IList<OutputTerminal> outputTerminals { get; set; }
        public Position position { get; set; }
        public string isFinished { get; set; }
        public string isRecurring { get; set; }
        public string segmentId { get; set; }
        public string waitFor { get; set; }
        public string testId { get; set; }
    }

    public class Root
    {
        public string type { get; set; }
        public string currentStatus { get; set; }
        public string id { get; set; }
        public IList<Item> items { get; set; }
    }

您可以创建类并序列化/反序列化json:

 public class OutputTerminal
    {
        public string type { get; set; }
        public string id { get; set; }
        public string connectedId { get; set; }
        public string terminalType { get; set; }
        public string connectedType { get; set; }
    }

    public class Position
    {
        public string type { get; set; }
        public string x { get; set; }
        public string y { get; set; }
    }

    public class Item
    {
        public string type { get; set; }
        public string id { get; set; }
        public string name { get; set; }
        public string memberCount { get; set; }
        public IList<OutputTerminal> outputTerminals { get; set; }
        public Position position { get; set; }
        public string isFinished { get; set; }
        public string isRecurring { get; set; }
        public string segmentId { get; set; }
        public string waitFor { get; set; }
        public string testId { get; set; }
    }

    public class Root
    {
        public string type { get; set; }
        public string currentStatus { get; set; }
        public string id { get; set; }
        public IList<Item> items { get; set; }
    }
希望能有帮助


希望它能有所帮助

尝试一下并感到惊讶,因为你今天学到了一些东西!尝试一下,并感到惊讶,因为你今天学到了一些东西!