反序列化JSON数组

时间:2014-06-13 16:04:18

标签: c# json parsing json.net

我正在尝试使用返回以下JSON结构的API:

{
  standardList: [2]
    0:  {
      created: "/Date(1393282097000)/"
      createdBy: "User Name"
      envId: "1234-1234-1234"
      extension: "nbsq"
      id: "4826-5680-3258"
      modified: "/Date(1328572097000)/"
      modifiedBy: "user Name"
      name: "File Name Here"
      officialVer: 1
      size: 1
      syncMod: 3210357275
      url: "https://www.example.com"
      versions: 1
    }-
    1:  {
      created: "/Date(139345635000)/"
      createdBy: "User Name"
      envId: "2345-2345-2345"
      extension: "nbsq"
      id: "4956-4538-4321"
      modified: "/Date(13934425017000)/"
      modifiedBy: "User Name"
      name: "File Name Here"
      officialVer: 1
      size: 1
      syncMod: 2812280243
      url: "https://www.example.com"
      versions: 1
    }
}

我需要反序列化standardList的内容并设置id和name值。我有一个模特:

public class FolderList
{
    [JsonProperty("standardList")]
    public Folder FolderList { get; set; }
}

public class Folder
{ 
  [JsonProperty("envId")]
  public string Id {get; set;}
  [JsonProperty("name")]
  public string Name {get;set;}
}

我尝试按照建议here进行操作并运行

JsonConvert.DeserializeObject<FolderList<Folder>>(json);

但它不起作用,因为响应没有每个项目的standardList键,它是一个数组。如何反序列化为数组?

编辑:实际JSON

{"standardList":[{"created":"\/Date(13963113097000)\/","createdBy":"User Name","envId":"1234-1234-1234","extension":"nbsq","id":"5326-0160-3098","modified":"\/Date(1396323497000)\/","modifiedBy":"User Name","name":"File Name Here","officialVer":1,"size":1,"syncMod":32350008075,"url":"https:\/\/www.example.com","versions":1},{"created":"\/Date(1334556017000)\/","createdBy":"User Name","envId":"1234-1234-1234","extension":"nbsq","id":"4728-1586-4633","modified":"\/Date(13933576017000)\/","modifiedBy":"User Name","name":"File Name Here","officialVer":1,"size":1,"syncMod":2822280170,"url":"https:\/\/www.example.com","versions":1}]}

1 个答案:

答案 0 :(得分:3)

转到Json2csharp.com。传递一些有效的JSON。引用项目中生成的类,然后反序列化。首先通过转到jsonlint来获取您的JSON。应该是一种享受。