将JSON转换为C#对象

时间:2019-07-04 02:58:57

标签: c# .net json json.net

我需要将以下JSON转换为C#对象

JSON:

{
    "Order": {
      "OrderID": "15791298",

      "Stops": {

        "Stop": {
          "StopID": "001",
          "Sequence": "1",
        },

        "Stop": {
          "StopID": "002",
          "Sequence": "2",
        }

      }
    }
}

需要转换为:

public class RootObject
{
    public string Order { get; set; }
}

public class Order
{
    public string OrderID { get; set; }
    public List<Stop> Stops {get; set; }
}

public class Stop 
{
    public string StopID { get; set; }
    public string Sequence { get; set; }
}

尝试使用以下代码反序列化此JSON:

RootObject order = JsonConvert.DeserializeObject<RootObject>(result);

出现此错误:

Newtonsoft.Json.JsonSerializationException:'无法反序列化当前JSON对象(例如{“ name”:“ value”})为类型'System.Collections.Generic.List`

如何将JSON转换为这些类?

0 个答案:

没有答案