我有这个 json 对象 - 在 view monde
[{
"Urltitle": "",
"location": "",
"Url": ""
}, {
"Urltitle": "",
"location": "",
"Url": ""
}, {
"Urltitle": "",
"location": "",
"Url": ""
}]
班级
namespace Models
{
public partial class Urls
{
public string Urltitle { get; set; }
public string Location { get; set; }
public string Url { get; set; }
}
}
使用这个解串器
IList<T> ssdResponse = JsonSerializer.Deserialize<IList<T>>(content, JsonOptions);
但是一直报错:
: 'The JSON value could not be converted to System.Collections.Generic.List`1[Models.Urls]. Path: $[0] | LineNumber: 0 |
我已经尝试过在线解决方案,但无法成功映射 - 我是否遗漏了什么?
我已经为您的场景创建了一个快速的 .NET Fiddle。对于您提供的示例,反序列化工作正常。
https://dotnetfiddle.net/4C6HEw
using System;
using System.Text.Json;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
string s = """"
[{
"Urltitle": "",
"location": "",
"Url": ""
}, {
"Urltitle": "",
"location": "",
"Url": ""
}, {
"Urltitle": "",
"location": "",
"Url": ""
}]
"""";
var list = JsonSerializer.Deserialize<List<Urls>>(s);
Console.WriteLine(JsonSerializer.Serialize(list));
}
public partial class Urls
{
public string Urltitle { get; set; }
public string location { get; set; }
public string Url { get; set; }
}
}
您需要检查您的实际 JSON(或类)是否具有稍微不同的结构等,这会导致反序列化问题。