英文:
Converting JSON to C# class deserializer errors on List
问题
I have this JSON object - in view mode
[{
"Urltitle": "",
"location": "",
"Url": ""
}, {
"Urltitle": "",
"location": "",
"Url": ""
}, {
"Urltitle": "",
"location": "",
"Url": ""
}]
Class:
namespace Models
{
public partial class Urls
{
public string Urltitle { get; set; }
public string Location { get; set; }
public string Url { get; set; }
}
}
Use this deserializer:
IList<T> ssdResponse = JsonSerializer.Deserialize<IList<T>>(content, JsonOptions);
But Keep getting error:
The JSON value could not be converted to System.Collections.Generic.List<Models.Urls>. Path: $[0] | LineNumber: 0 |
I've tried solutions online but cannot get this successfully mapped - am I missing something?
英文:
I have this JSON object - in view mode
[{
"Urltitle": "",
"location": "",
"Url": ""
}, {
"Urltitle": "",
"location": "",
"Url": ""
}, {
"Urltitle": "",
"location": "",
"Url": ""
}]
Class:
namespace Models
{
public partial class Urls
{
public string Urltitle { get; set; }
public string Location { get; set; }
public string Url { get; set; }
}
}
Use this deserializer:
IList<T> ssdResponse = JsonSerializer.Deserialize<IList<T>>(content, JsonOptions);
But Keep getting error:
> 'The JSON value could not be converted to System.Collections.Generic.List`1[Models.Urls]. Path: $[0] | LineNumber: 0 |
I've tried solutions online but cannot get this successfully mapped - am I missing something?
答案1
得分: 1
I've created a quick .NET Fiddle for your scenario. The deserialization works fine for the example you provided.
https://dotnetfiddle.net/4C6HEw
using System;
using System.Text.Json;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
string s = "Your JSON data here"; // Replace with your JSON data
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; }
}
}
You'd need to check if your actual JSON (or class) has a slightly different structure, etc., that would cause an issue with deserialization.
英文:
I've created a quick .NET Fiddle for your scenario. The deserialization works fine for the example you provided.
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; }
}
}
You'd need to check if your actual JSON (or class) has a slightly different structure etc. that would cause issue with deserialization.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论