将JSON转换为C#类反序列化器在List上出现错误。

huangapple go评论69阅读模式
英文:

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

[{
	&quot;Urltitle&quot;: &quot;&quot;,
	&quot;location&quot;: &quot;&quot;,
	&quot;Url&quot;: &quot;&quot;
}, {
	&quot;Urltitle&quot;: &quot;&quot;,
	&quot;location&quot;: &quot;&quot;,
	&quot;Url&quot;: &quot;&quot;
}, {
	&quot;Urltitle&quot;: &quot;&quot;,
	&quot;location&quot;: &quot;&quot;,
	&quot;Url&quot;: &quot;&quot;
}]

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&lt;T&gt; ssdResponse = JsonSerializer.Deserialize&lt;IList&lt;T&gt;&gt;(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 = &quot;&quot;&quot;&quot;
			[{
				&quot;Urltitle&quot;: &quot;&quot;,
				&quot;location&quot;: &quot;&quot;,
				&quot;Url&quot;: &quot;&quot;
			}, {
				&quot;Urltitle&quot;: &quot;&quot;,
				&quot;location&quot;: &quot;&quot;,
				&quot;Url&quot;: &quot;&quot;
			}, {
				&quot;Urltitle&quot;: &quot;&quot;,
				&quot;location&quot;: &quot;&quot;,
				&quot;Url&quot;: &quot;&quot;
			}]
			&quot;&quot;&quot;&quot;;
		var list = JsonSerializer.Deserialize&lt;List&lt;Urls&gt;&gt;(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.

huangapple
  • 本文由 发表于 2023年4月20日 05:41:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定