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

C# 在C中读取JSON文件时遇到问题#,c#,json,json.net,deserialization,C#,Json,Json.net,Deserialization,我是C#的新手,在阅读JSON文件时遇到问题。 JSON文件采用以下格式: { "info": { "year": 2020, "version": "1.0", "description": "fake description", "date_created": "2020-04-31T20:32:11.895

我是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文件中的Images部分。这是我的课程:

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; }
}
在我的主类中,我尝试在这里反序列化它:

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

使用系统;
使用System.Collections.Generic;
使用System.IO;
使用Newtonsoft.Json;
命名空间C_
{
公共类read_json
{
静态void Main(字符串[]参数)
{
使用(StreamReader r=newstreamreader(“COCOExport.json”))
{
字符串json=r.ReadToEnd();

var test1=JsonConvert.DeserializeObject您的问题应该在这里:

public class Image_json
{
    [JsonProperty("images")]
    public Image_json Image_json { get; set; }
}
此属性的类型为
Image\u json
,与包含的类相同。您需要一组
图像
。可以是数组或
列表

public-class-Image\u-json
{
[JsonProperty(“图像”)]
公共列表映像_json{get;set;}
}

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

请查找下面的代码

类图像:

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; }
}
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();
    }
类ImageJson:

 public class ImageJson
{
    [JsonProperty("images")]
    public List<Images> Image_json // We shoild use list of images
    {
        get; set;
    }
}
公共类ImageJson
{
[JsonProperty(“图像”)]
公共列表图像\u json//我们应该使用图像列表
{
获得;设置;
}
}
测试输出:

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; }
}
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();
    }
使用(StreamReader r=newstreamreader(“COCOExport.json”))
{
字符串json=r.ReadToEnd();
var test1=JsonConvert.DeserializeObject(json);
foreach(test1.Image_json中的var输出)
{
Console.WriteLine(output.id);
控制台写入线(输出宽度);
控制台写入线(输出高度);
Console.WriteLine(output.filename);
Console.WriteLine(output.license);
}
Console.ReadLine();
}
对属性使用PascalCasing一直是一个很好的实践

例:) [JsonProperty(“id”)] 公共int Id{get;set;} 请在这里找到解决方案。在视频中解释

链接的“相关”问题是重复的。您的根JSON值是一个对象而不是数组,因此您需要反序列化到
Image\u JSON
not
List
如下:
JsonConvert.DeserializeObject(JSON)
正如我在帖子中所说,我遵循了该解决方案,收到了以下错误:未处理的异常。System.NullReferenceException:对象引用未设置为对象的实例。问题中显示的代码未编译,请参阅:
编译错误(第39行,第23列):'Image_json':成员名称不能与其封闭类型相同。
。下面的答案显示了如何修复编译,因此请尝试使用该+链接的答案。如果这些答案不起作用,请将您的问题与我们可以编译的代码共享,以复制异常。您的模型完全不正确。您需要修复您的模型,so这反映了您的JSON的结构。修复了示例,使其包含最小的可重复示例。请尝试改进您的答案,以便您解释为什么您的解决方案是一个解决方案以及它是如何工作的。不要链接到任何包含视频的youtube频道,而是尝试以文本形式解释此平台上的所有内容。