skip to Main Content

trying to read and process Json file and getting that eror:
System.Text.Json.JsonException: ‘The JSON value could not be converted to System.Collections.Generic.List`1[ConsoleApp3.match]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.’

What is the problem with the Json file?
Would appreciate help

internal class Program
{
    static void Main(string[] args)
    {
       var incoming = new List<match>();
        using (StreamReader r = new StreamReader("data.json"))

        {

            string json = r.ReadToEnd();
            incoming = JsonSerializer.Deserialize<List<match>>(json);

        }
        if (incoming != null && incoming.Count > 0) {  
            foreach (var score in incoming)
            {

                Console.WriteLine($"{score.home_name} {score.away_name}");

            }

       
        } 
    
        Console.ReadLine();


        }


}
            public class match
    { 
        public int id { get; set; }
    public string home_name { get; set; }
    public string away_name { get; set; }
    public string score { get; set; }
    public int time { get; set; }
    public int league_id { get; set; }
    public string status { get; set; }



}


 



    }

JSON FILE:

"match": [


{

  "id": "28172",

  "home_name": "Flamengo",

  "away_name": "Palestino",

  "score": "0 - 1",

  "time": "36",

  "league_id": "65",

  "status": "IN PLAY"

},

{

  "id": "28173",

  "home_name": "Santa Cruz",

  "away_name": "Independiente Medellin",

  "score": "2 - 0",

  "time": "36",

  "league_id": "65",

  "status": "IN PLAY"

},

{

  "id": "28174",

  "home_name": "Union de Sunchales",

  "away_name": "Gimnasia y Esgrima de Concepcion",

  "score": "0 - 0",

  "time": "58",

  "league_id": "166",

  "status": "IN PLAY"

},

{

  "id": "28175",

  "home_name": "Corinthians",

  "away_name": "Cruzeiro",

  "score": "0 - 0",

  "time": "34",

  "league_id": "128",

  "status": "IN PLAY"

},

{

  "id": "28176",

  "home_name": "Gremio",

  "away_name": "Palmeiras",

  "score": "1 - 0",

  "time": "36",

  "league_id": "128",

  "status": "IN PLAY"

},

{

  "id": "28177",

  "home_name": "Real Potosi",

  "away_name": "Bolivar",

  "score": "2 - 1",

  "time": "61",

  "league_id": "91",

  "status": "IN PLAY"

},

{

  "id": "28178",

  "home_name": "Blooming",

  "away_name": "San Jose",

  "score": "3 - 0",

  "time": "HT",

  "league_id": "91",

  "status": "HALF TIME BREAK"

},

{

  "id": "28179",

  "home_name": "Tachira",

  "away_name": "Llaneros Guanare",

  "score": "2 - 0",

  "time": "84",

  "league_id": "66",

  "status": "IN PLAY"

},

{

  "id": "28180",

  "home_name": "DC United",

  "away_name": "Columbus Crew",

  "score": "2 - 0",

  "time": "86",

  "league_id": "94",

  "status": "IN PLAY"

},

{

  "id": "28181",

  "home_name": "Montreal Impact",

  "away_name": "San Jose Earthquakes",

  "score": "2 - 1",

  "time": "84",

  "league_id": "94",

  "status": "IN PLAY"

},

{

  "id": "28182",

  "home_name": "Toronto FC",

  "away_name": "Orlando City",

  "score": "0 - 0",

  "time": "77",

  "league_id": "94",

  "status": "IN PLAY"

},

{

  "id": "28183",

  "home_name": "Carolina RailHawks",

  "away_name": "New York Cosmos",

  "score": "0 - 2",

  "time": "75",

  "league_id": "67",

  "status": "IN PLAY"

},

{

  "id": "28184",

  "home_name": "Puerto Rico FC",

  "away_name": "Fort Lauderdale Strikers",

  "score": "2 - 1",

  "time": "83",

  "league_id": "67",

  "status": "IN PLAY"

},

{

  "id": "28186",

  "home_name": "Miami FC",

  "away_name": "Ottawa Fury",

  "score": "1 - 1",

  "time": "54",

  "league_id": "67",

  "status": "IN PLAY"

}

]

}

2

Answers


  1. The JSON you’re showing is invalid. Because you missed the first character:

    {
    

    This is critical because this JSON represents an object, not an array. But you’re trying to deserialize it as an array:

    incoming = JsonSerializer.Deserialize<List<match>>(json);
    

    Instead, create a class which matches the object structure:

    public class Matches
    {
        public IEnumerable<Match> match { get; set; }
    }
    

    (Also fix the casing on your Match class to start with a capital letter. Properties should also be capitalized, but that’s not necessarily as important as classes and can be addressed another time.)

    Then deserialize as that object:

    incoming = JsonSerializer.Deserialize<Matches>(json);
    
    Login or Signup to reply.
  2. you have an object and after this an array, so you can parse your json and after this deserialize an array match to a list

    List<match> matches =  JsonNode.Parse(json)["match"].Deserialize<List<match>>();
    
    public class match
    {
        public string id { get; set; }
        public string home_name { get; set; }
        public string away_name { get; set; }
        public string score { get; set; }
        public string time { get; set; }
        public string league_id { get; set; }
        public string status { get; set; }
    }
    

    Since your json has only string values I fixed your class and made all properties string too. If you need the real types, you will need much more code since you are using System.Text.Json. IMHO Newtonsoft.Json is much better for your needs, you will not do any extra code

    using Newtonsoft.Json;
    
    List<match> matches =  JsonObject.Parse(json)["match"].ToObject<List<match>>();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search