在 C# 中读取 JSON 文件时遇到问题

Having issues reading a JSON file in C#

我是 C# 的新手,我在读取 JSON 文件时遇到问题。 JSON 文件遵循以下格式:

{
  "info": {
    "year": 2020,
    "version": "1.0",
    "description": "fake description",
    "date_created": "2020-04-31T20:32:11.8958473Z"
  },
  "licenses": [
    {
      "name": "fake name",
      "id": 2020
    }
  ],
  "images": [
    {
      "id": 1,
      "width": 1280,
      "height": 720,
      "filename": "filename1.jpeg",
      "license": 1
    },
    {
      "id": 2,
      "width": 1280,
      "height": 720,
      "filename": "filename2.jpeg",
      "license": 2
    },
    ...

现在我正在尝试阅读 JSON 文件中的图像部分。这是我的 class:

public class Images    
{
    [JsonProperty("id")]
    public int id { get; set; }
    [JsonProperty("width")] 
    public int width { get; set; } 
    [JsonProperty("height")] 
    public int height { get; set; } 
    [JsonProperty("filename")]
    public string filename { get; set; } 
    [JsonProperty("license")]
    public int license { get; set; } 
}

public class Image_json
{
    [JsonProperty("images")]
    public Image Image_json { get; set; }
}

在我的主要 class 中,我尝试在此处反序列化它:

using System;
using System.Collections.Generic;
using System.IO;


using Newtonsoft.Json;

namespace C__Playground
{
    public class read_json
    {
        static void Main(string[] args)
        {
            using (StreamReader r = new StreamReader("COCOExport.json"))
            {
                string json = r.ReadToEnd();
                var test1 = JsonConvert.DeserializeObject<List<Image_json>(json);
            }
        }
    }
}

当我尝试 运行 程序时,我收到此消息:

Unhandled exception. Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[C__Playground.Image_jsonJson]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

我一直在关注这个 post 。 我尝试使用解决方案 here 但它 returns 为空或者我收到空错误。有什么见解吗?

你的问题应该在这里:

public class Image_json
{
    [JsonProperty("images")]
    public Image_json Image_json { get; set; }
}

此 属性 的类型为 Image_json,与包含 class 的类型相同。您需要 Images 的集合。可以是数组或 List<Images>.

public class Image_json
{
    [JsonProperty("images")]
    public List<Images> Image_json { get; set; }
}

顺便说一句,Images class 应该被称为 Image 因为它包含单个图像,而不是它们的集合。

请查找以下代码。

Class 图片:

public class Images    
{
    [JsonProperty("id")]
    public int id { get; set; }
    [JsonProperty("width")]
    public int width { get; set; }
    [JsonProperty("height")]
    public int height { get; set; }
    [JsonProperty("filename")]
    public string filename { get; set; }
    [JsonProperty("license")]
    public int license { get; set; }
}

class ImageJson:

 public class ImageJson
{
    [JsonProperty("images")]
    public List<Images> Image_json // We shoild use list of images
    {
        get; set;
    }
}

测试输出:

using (StreamReader r = new StreamReader("COCOExport.json"))
    {
        string json = r.ReadToEnd();
        var test1 = JsonConvert.DeserializeObject<ImageJson>(json);

        foreach(var output in test1.Image_json)
        {
            Console.WriteLine(output.id);
            Console.WriteLine(output.width);
            Console.WriteLine(output.height);
            Console.WriteLine(output.filename);
            Console.WriteLine(output.license);
        }
        Console.ReadLine();
    }

对属性使用 PascalCasing 始终是一个好习惯。

例如:) [JsonProperty("id")] public int Id { 得到;放; } 请在此处找到解决方案。在视频中解释 https://www.youtube.com/watch?v=nHtdReIhvag