Converting Json to C# Class Desrializer errors on List

问题描述 投票:0回答:1

我有这个 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 | 

我已经尝试过在线解决方案,但无法成功映射 - 我是否遗漏了什么?

c# json json-deserialization jsonserializer
1个回答
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(或类)是否具有稍微不同的结构等,这会导致反序列化问题。

© www.soinside.com 2019 - 2024. All rights reserved.