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.

3
  • 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 such as json2csharp.com or app.quicktype.io and you will get a correct data model. Commented Jul 30, 2021 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; } } Commented Jul 30, 2021 at 12:59
  • I copied the auto-generated classes from json2csharp.com into dotnetfiddle.net/UKxxpB and your JSON can be deserialized successfully. json2csharp does the same thing as the answer by user3026017, which is to add an extra level of object with a public Navigationlink Navigationlink { get; set; } property. Closing as a duplicate. Commented Jul 31, 2021 at 1:07

1 Answer 1

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());
Sign up to request clarification or add additional context in comments.

2 Comments

But Links isn't a jsonObject it's a jsonArray
NavigationLinks contains the array of Links..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.