尝试读取和处理Json文件并获得该错误:System.Text.Json.JsonException: 'JSON值无法转换为系统.集合.通用.列表' 1 [ConsoleApp3.match]。路径:$|行号:0|行内字节位置:1.'
Json文件有什么问题?希望得到帮助
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文件:
"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条答案
按热度按时间ve7v8dk21#
您显示的JSON无效。因为您缺少第一个字符:
这一点很关键,因为这个JSON表示的是一个 object,而不是一个 array,但是您试图将它反序列化为一个数组:
相反,创建一个与对象结构匹配的类:
(Also修改
Match
类的大小写,使其以大写字母开头,属性也应该大写,但这不一定像类那样重要,可以在另一个时间解决。)然后反序列化为该对象:
7gcisfzg2#
你有一个对象,然后是一个数组,所以你可以解析你的json,然后反序列化一个数组匹配到列表
因为你的json只有字符串值,所以我修改了你的类,把所有的属性也变成字符串。如果你需要真实的的类型,你将需要更多的代码,因为你使用的是System.Text.Json。恕我直言Newtonsoft.Json更适合你的需要,你不会做任何额外的代码