简体   繁体   English

将JSON解析为对象

[英]Parse JSON to object

I am working with box api, and trying to parse json object to a class. 我正在使用box api,并尝试将json对象解析为一个类。
This is the 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":"a@gmail.com"
  },
  "trashed_at":null,
  "purged_at":null,
  "content_created_at":null,
  "content_modified_at":null,
  "owned_by":
  {
    "type":"user",
    "id":"111",
    "name":"a a",   
    "login":"a@gmail.com"   
  },    
  "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"
      }
    ]
  }
}

Basically, this is the root folder (in that case) that contains two files: 基本上,这是包含两个文件的根文件夹(在这种情况下):
13.ppt 13.ppt
6.docx 6.docx
I created a class: 我创建了一个类:

[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; }
}  

But the "item_collection" part is not working.. it gives me an error.. 但是“ item_collection”部分不起作用..它给我一个错误..

How do I get a list of subfiles inside "item_collection"? 如何获取“ item_collection”中的子文件列表?

I use it by: 我通过以下方式使用它:

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

And: 和:

BoxFile parsed = ParseJson<BoxFile>(json);

You are getting an error because your class structure does not match your JSON. 您收到错误消息,因为您的类结构与JSON不匹配。 Specifically, in the JSON, the item_collection property is an object, not a list. 具体来说,在JSON中, item_collection属性是一个对象,而不是列表。 That JSON object has two properties, total_count and entries , the latter of which contains the actual list of files. 该JSON对象具有两个属性total_countentries ,后者包含文件的实际列表。 To handle this, you need to define another class: 要处理此问题,您需要定义另一个类:

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

and then change the ItemCollection property in your BoxFile class to use this new class: 然后将BoxFile类中的ItemCollection属性更改为使用此新类:

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

You can then access the list of files like this: 然后,您可以像这样访问文件列表:

BoxFile parsed = ParseJson<BoxFile>(json);

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

Here is a working demo: https://dotnetfiddle.net/DB9Coc 这是一个有效的演示: https : //dotnetfiddle.net/DB9Coc

As an aside, you can simplify your ParseJson method to one line. ParseJson ,您可以将ParseJson方法简化为一行。 There is no need to parse the JSON to an JObject , turn it back into JSON and then parse it again. 无需将JSON解析为JObject ,然后将其转换回JSON,然后再次解析。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM