将JSON解析为对象

用户名

我正在使用box api,并尝试将json对象解析为一个类。
这是json:

{
  "type":"folder",
  "id":"0",
  "sequence_id":null,
  "etag":null,
  "name":"All Files",
  "created_at":null,
  "modified_at":null,
  "description":"",
  "size":9049537,
  "path_collection":
  {
    "total_count":0,"entries":[]
  },
  "created_by":
  {
    "type":"user","id":"","name":"","login":""
  },
  "modified_by":
  {
    "type":"user",
    "id":"111",
    "name":"a a",
    "login":"[email protected]"
  },
  "trashed_at":null,
  "purged_at":null,
  "content_created_at":null,
  "content_modified_at":null,
  "owned_by":
  {
    "type":"user",
    "id":"111",
    "name":"a a",   
    "login":"[email protected]"   
  },    
  "shared_link":null,
  "folder_upload_email":null,
  "parent":null,
  "item_status":"active",
  "item_collection":
  {
    "total_count":4,
    "entries":
    [
      {
        "type":"file",
        "id":"22887167395",
        "sequence_id":"0",
        "etag":"0",
        "sha1":"883c99863eefc0f46b3d34915cc4d97a6008fabf",
        "name":"13.ppt"
      },
      {
        "type":"file",
        "id":"22887169687",
        "sequence_id":"0",
        "etag":"0",
        "sha1":"a345fd68b1c90a3678a3e746e0e5343693d8a022",
        "name":"6.docx"
      }
    ],
    "offset":0,
    "limit":100,
    "order":
    [
      {
        "by":"type",
        "direction":"ASC"
      },
      {
        "by":"name",
        "direction":"ASC"
      }
    ]
  }
}

基本上,这是一个包含两个文件的根文件夹(在这种情况下):
13.ppt
6.docx
我创建了一个类:

[JsonObject(MemberSerialization.OptIn)]
public class BoxFile
{
    [JsonProperty(PropertyName = "type")]
    public string Type { get; internal set; }

    [JsonProperty(PropertyName = "id")]
    public string Id { get; internal set; }

    [JsonProperty(PropertyName = "sequence_id")]
    public string SequenceId { get; internal set; }

    [JsonProperty(PropertyName = "etag")]
    public string Etag { get; internal set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; internal set; }

    [JsonProperty(PropertyName = "created_at")]
    public string CreatedAt { get; internal set; }

    [JsonProperty(PropertyName = "modified_at")]
    public string ModifiedAt { get; internal set; }

    [JsonProperty(PropertyName = "description")]
    public string Description { get; internal set; }

    [JsonProperty(PropertyName = "size")]
    public long Size { get; internal set; }

    [JsonProperty(PropertyName = "item_collection")]
    public IEnumerable<BoxFile> ItemCollection { get; internal set; }
}  

但是“ item_collection”部分不起作用..它给我一个错误..

如何获取“ item_collection”中的子文件列表?

我通过以下方式使用它:

    private T ParseJson<T>(string json) where T : class, new()
    {
            JObject jobject = JObject.Parse(json);
            return JsonConvert.DeserializeObject<T>(jobject.ToString());
    }  

和:

BoxFile parsed = ParseJson<BoxFile>(json);
布莱恩·罗杰斯(Brian Rogers)

您收到错误消息,因为您的类结构与JSON不匹配。具体来说,在JSON中,item_collection属性是一个对象,而不是列表。该JSON对象具有两个属性,total_countentries,后者包含实际的文件列表。要处理此问题,您需要定义另一个类:

public class ItemCollection
{
    [JsonProperty(PropertyName = "entries")]
    public IEnumerable<BoxFile> Entries { get; internal set; }
}

然后更改ItemCollection您的BoxFile类中属性以使用此新类:

    [JsonProperty(PropertyName = "item_collection")]
    public ItemCollection ItemCollection { get; internal set; }

然后,您可以访问文件列表,如下所示:

BoxFile parsed = ParseJson<BoxFile>(json);

foreach (BoxFile file in parsed.ItemCollection.Entries)
{
    Console.WriteLine(file.Name);
}

这是一个有效的演示:https : //dotnetfiddle.net/DB9Coc

顺便说一句,您可以将ParseJson方法简化为一行。无需将JSON解析为JObject,然后将其转换回JSON,然后再次解析。

private T ParseJson<T>(string json) where T : class, new()
{
    return JsonConvert.DeserializeObject<T>(json);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章