0

Here is my JSON. It is valid according to https://jsonlint.com/

[{"page":1,"pages":1,"per_page":"50","total":1},[{"id":"GBR"}]]

So this looks very simple. It's based on data from the WorldBank, but I have simplified it. Here are my POCO objects:

    public class Rootobject
    {
        public PagesObject Pages { get; set; }        

        public List<WorldBankCountryContainer> NestedListContainer {get; set;}
        
    }

    public class WorldBankCountryContainer
    {
        [JsonProperty("id")]
        public string id { get; set; }

    }

    public class PagesObject
    {
        [JsonProperty("page")]
        public int Page { get; set; }

        [JsonProperty("pages")]
        public int Pages { get; set; }

        [JsonProperty("per_page")]
        public string PerPage { get; set; }

        [JsonProperty("total")]
        public long Total { get; set; }
    }

And I cannot deserialize it like this :

var countryData = JsonSerializer.Deserialize<List<Rootobject>[]>(json);

(The above will work without the 'pages' anonynmous object in the JSON.)

user1970636
  • 11
  • 1
  • 2
  • just because it's valid doesn't mean it's usable in a practical sense... Just because you can jump off the empire state building doesn't mean you should. – Andy Sep 05 '20 at 15:17
  • ```http://api.worldbank.org/v2/country/br?format=json``` so I cannot get the worldBank to change, and it is valid, so it can and should be able to be deserialized in a general purpose programming language like C#. – user1970636 Sep 05 '20 at 15:28
  • What's wrong with this is they used an array as the container format. That isn't practical. There are zero reasons to use an array. If it only ever will be 2 distinct data structures inside this array, then there is no reason for it. This is poorly designed JSON. Again, just because it's valid, doesn't mean it's practical. – Andy Sep 05 '20 at 15:31
  • Anything can be deserialized. But this structure is going to be a pain. You will have to create a generic object array, then inspect each object to see if they have certain properties, then deserialize those based on the properties. – Andy Sep 05 '20 at 15:34
  • What serializer are you using? You tagged your question [tag:json.net] But `JsonSerializer.Deserialize` isn't a static method of the Json.NET serializer, it looks more like [tag:system.text.json]. – dbc Sep 05 '20 at 15:37
  • If you are using [tag:json.net] this looks to be a duplicate of [c# Deserialize unlabelled JSON array](https://stackoverflow.com/q/52743860/3744182) and [How to deserialize an array of values with a fixed schema to a strongly typed data class?](https://stackoverflow.com/a/39462464/3744182). Demo fiddle here: https://dotnetfiddle.net/jJSvja – dbc Sep 05 '20 at 15:39
  • Correct I am using the native Json but the same thing occurs using Newtonsoft – user1970636 Sep 05 '20 at 15:41
  • 1
    @dbc just posted the source code to your problem: https://dotnetfiddle.net/jJSvja same data structure and everything. – Andy Sep 05 '20 at 15:42
  • @user1970636 - then please clarify whether you need a [tag:json.net] answer or a [tag:system.text.json] answer. Since a complex converter is required, [tag:json.net] will be easier to use -- especially since there's already a Q&A on stack overflow with the necessary converter. – dbc Sep 05 '20 at 15:44
  • Thanks all for your help. Either json.net or system.text.json. I would presume the json.net may be more suitable. – user1970636 Sep 05 '20 at 15:48
  • @user1970636 - So shall I mark this as a duplicate of the previous two questions to which I linked, or do you need more specific help? – dbc Sep 05 '20 at 15:51
  • 1
    @dbc - well this question has a more concise Json structure but yeah it's a duplicate. – user1970636 Sep 05 '20 at 16:00

0 Answers0