-3

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?

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
enavuio
  • 1,428
  • 2
  • 19
  • 31
  • The name in your error message is different from the name of the class you included – stuartd Apr 19 '23 at 21:50
  • `Path: $[0] | LineNumber: 0` - are you sure that’s the JSON you’re actually trying to deserialise? Seeing an error in the very first character suggests it’s something else – stuartd Apr 19 '23 at 23:23

1 Answers1

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 = """"
            [{
                "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.

R J
  • 495
  • 5
  • 12