0

I'm facing this json data:

{
    "pagemeta": {
        "slug": "/",
        "title": "test",
        "description": "test"
    },
    "navigationlinks": {
        "links": [{
            "navigationlink": {
                "name": "Index.",
                "url": "/"
            }
        }]
    }
}

And both using HttpRespondeMessage.Content.ReadAsJsonAsync<T>(); or JsonConvert.DeserializeAnonymousType(jsonString, new MyClassToDeserializeInto()) Both parsings seem to function well, except when it comes to the objects inside the array "links".

Those do get to become object in the array in class but all data is null(both name and url like in this example).

Am I doing something wrong to don't get the json deserialized correctly?

Following are the classes that I use as the final target object:

HomeData.cs

public sealed class HomeData
    {
        [JsonProperty("pagemeta")]
        public PageMeta PageMeta { get; set; }
        [JsonProperty("navigationlinks")]
        public NavigationLinks NavigationLinks { get; set; }
    }

NavigationLinks.cs

public class NavigationLinks
    {
        [JsonProperty("links")]
        public NavigationLink[] Links { get; set; }
    }

NavigationLink.cs

public class NavigationLink
    {
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("url")]
        public string Url { get; set; }
    }

meanwhile the PageMeta.cs does the data correctly.

Eugene
  • 217
  • 8
  • 24
  • Your data model is not correct. Upload your JSON to one of the tools suggested in [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/q/21611674/3744182) such as https://json2csharp.com/ or https://app.quicktype.io/ and you will get a correct data model. – dbc Jul 30 '21 at 12:57
  • As shown by those tools you need an extra level of container with a `"navigationlink"` property, e.g. `public partial class Navigationlinks { [JsonProperty("links")] public Link[] Links { get; set; } }` which contains the extra level **`public partial class Link { [JsonProperty("navigationlink")] public Navigationlink Navigationlink { get; set; } }`** – dbc Jul 30 '21 at 12:59
  • I copied the auto-generated classes from https://json2csharp.com/ into https://dotnetfiddle.net/UKxxpB and your JSON can be deserialized successfully. json2csharp does the same thing as [the answer](https://stackoverflow.com/a/68591919/3744182) by [user3026017](https://stackoverflow.com/users/3026017/user3026017), which is to add an extra level of object with a `public Navigationlink Navigationlink { get; set; }` property. Closing as a duplicate. – dbc Jul 31 '21 at 01:07

1 Answers1

1

You have missed the Links class, and mark the Links class as JsonObject

public sealed class HomeData
{
    [JsonProperty("pagemeta")]
    public PageMeta PageMeta { get; set; }
    [JsonProperty("navigationlinks")]
    public NavigationLinks NavigationLinks { get; set; }
}

public class PageMeta
{
    [JsonProperty("slug")]
    public string Slug { get; set; }

    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }
}

public class NavigationLink
{
    [JsonProperty("name")]
    public string Name { get; set; }
    [JsonProperty("url")]
    public string Url { get; set; }
}

public class NavigationLinks
{
    [JsonProperty("links")]
    public Links[] Links { get; set; }
}

[JsonObject]
public class Links
{
    [JsonProperty("navigationlink")]
    public NavigationLink NavigationLink { get; set; }
}

And then use it like this :

var homedata = JsonConvert.DeserializeAnonymousType(text, new HomeData());
user3026017
  • 569
  • 4
  • 10