Search code examples
c#jsonjson.netdeserialization

Having issues reading a JSON file in C#


I am a novice in C# and I am having issues reading a JSON file. The JSON file follows this format:

{
  "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
    },
    ...

For now I am trying to read the Images section in the JSON file. Here is my class for it:

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

In my main class, I try deserializing it here:

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

When I try to run the program, I get this message:

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.

I have been following this post here. I tried using the solution here but it returns empty or I receive a null error. Any insights?


Solution

  • Your problem should be here:

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

    This property is of type Image_json, which is the same as the containing class. You need a collection of Images. Could be an array or a List<Images>.

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

    BTW, the Images class should be called Image since it holds a single image, not a collection of them.