简体   繁体   English

C#Newtonsoft反序列化JSON数组

[英]C# Newtonsoft deserialize JSON array

I'm trying to deserialize an array using Newtonsoft so i can display files from a cloud based server in a listbox but i always end up getting this error no matter what i try: 我正在尝试使用Newtonsoft对数组进行反序列化,因此我可以在列表框中显示来自基于云的服务器的文件,但无论我尝试什么,我总是会收到此错误:

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: [. Newtonsoft.Json.JsonReaderException:'解析值时遇到意外的字符:[。 Path '[0].priv', line 4, position 15.' 路径'[0] .priv',第4行,第15位。'

Thisis an example try to deserialize: 这是一个尝试反序列化的示例:

[
 {
  "code": 200,
  "priv": [
     {
        "file": "file.txt",
        "ext": "txt",
        "size": "104.86"
     },
     {
        "file": "file2.exe",
        "ext": "exe",
        "size": "173.74"
     },

  ],
  "pub": [
     {
        "file": "file.txt",
        "ext": "txt",
        "size": "104.86"
     },
     {
        "file": "file2.exe",
        "ext": "exe",
        "size": "173.74"
     }
  ]
 }
]

I tried using a C# Class like this: 我尝试使用像这样的C#类:

    public class ListJson
{
    [JsonProperty("pub")]
    public List List { get; set; }
}

public class List
{
    [JsonProperty("file")]
    public string File { get; set; }

    [JsonProperty("ext")]
    public string Ext { get; set; }

    [JsonProperty("size")]
    public string Size { get; set; }
}
    [JsonProperty("priv")]
public List List { get; set; }
}

public class List
{
    [JsonProperty("file")]
    public string File { get; set; }

    [JsonProperty("ext")]
    public string Ext { get; set; }

    [JsonProperty("size")]
    public string Size { get; set; }
}

And deserialize with: 并反序列化:

List<list> fetch = Newtonsoft.Json.JsonConvert.DeserializeObject<List<list>>(json);

The correct C# class structure for your JSON is the following: JSON的正确C#类结构如下:

public class FileEntry
{
    public string file { get; set; }
    public string ext { get; set; }
    public string size { get; set; }
}

public class FileList
{
    public int code { get; set; }
    public List<FileEntry> priv { get; set; }
    public List<FileEntry> pub { get; set; }
}

Deserializing it in this way: 以这种方式反序列化:

var fetch = JsonConvert.DeserializeObject<FileList[]>(json);
var fileList = fetch.First(); // here we have a single FileList object

As said in the other answer, creating a class called List doesn't automagically turn it into a collection of objects. 如在另一个答案中所述,创建一个名为List的类不会自动将其转换为对象集合。 You need to declare the types to be deserialized from an array a collection type (eg List<T> , T[] , etc.). 您需要从数组中声明要反序列化的类型集合类型(例如List<T>T[]等)。

Small tip: when in doubt, use json2csharp.com to generate strongly typed classes from a json string. 小提示:如有疑问,请使用json2csharp.com从json字符串生成强类型类。

At the moment List has a single List instance called priv , which despite the name: doesn't make it a list. 目前, List有一个名为priv List实例,尽管名称为:不会使其成为列表。 To deserialize a JSON array ( "priv": [...] ), it needs to an array or list-like type, for example List<T> for some T . 要反序列化JSON数组( "priv": [...] ),它需要一个数组或类似列表的类型,例如某些T List<T> Presumably a List<FileThing> , if we assume that FileThing is actually the second type called List (you have 2). 如果我们假设FileThing实际上是名为List的第二种类型(你有2),那么可能是List<FileThing>

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

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