-2

from Postman

Hi guys I would like to ask for some help on how to deserialize an indefinite number of json array? Like this returned json array object contains 170 arrays. Here is my code for json array returned object

`public class HistoryData
    {
        public string date_time { get; set; }
        public string url { get; set; }
        public string browser_source { get; set; }
    }
    public class InsideDataArray
    {
        public HistoryData[] data { get; set; }
    }
    public class DataArray
    {
        public InsideDataArray[] data { get; set; }
    }
    
    public class BrowsingDataObject
    {
        public string api_msg { get; set; }
        public string api_code { get;set; }
        public string api_status { get; set; }

        public DataArray data { get; set; }
    }'
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • Please can you advise what issue you're facing? – ProgrammingLlama Sep 14 '21 at 03:04
  • hi @Llama I would like to know how to deserialize that kind of returned json nested array, I can't figure it out. – jecs cor Sep 14 '21 at 03:08
  • `BrowsingDataObject` seems to contain an object `Data`, which contains an array `data`, whose items themselves contains an array `Data`. I would expect the JSON to therefore look like this: `{ "api_msg": "abc", "api_code": "def", "api_status": "ghi", "data": { "data": [ { "data": [ { "date_time": "2021-09-09", "url": "https://www.google.com/", "browser_source": "Google" } ] } ] } }`. That looks absolutely nothing like the rendered JSON tree you have in the question. – ProgrammingLlama Sep 14 '21 at 03:09
  • JSON object (`{ ... }`) -> C# class. JSON array (`[...]`) -> C# collection type (list, array, ienumerable, etc.). – ProgrammingLlama Sep 14 '21 at 03:11
  • How can I set the name and number of arrays based from the JSON object in C# @Llama Thanks in advance. – jecs cor Sep 14 '21 at 03:12
  • The name comes from the property name - you already seem to know this. In the JSON tree you showed, you only have on array so your question is unclear. – ProgrammingLlama Sep 14 '21 at 03:13
  • I suggest following the advice [here](https://stackoverflow.com/a/46825163/3181933) and trying again. – ProgrammingLlama Sep 14 '21 at 03:15
  • Thank you @Llama, I followed the Question and all the answers and finally figured it out. Thank you so much for your help! [here](https://stackoverflow.com/questions/8734413/declaring-and-using-global-arrays-c-sharp) – jecs cor Sep 14 '21 at 03:38

1 Answers1

1

It looks like you can simplify to this:

    public class HistoryData
    {
        public string date_time { get; set; }
        public string url { get; set; }
        public string browser_source { get; set; }
    }
    
    
    public class BrowsingDataObject
    {
        public string api_msg { get; set; }
        public string api_code { get;set; }
        /* this should maybe be bool? */
        public string api_status { get; set; }

        public HistoryData[] data { get; set; }
    }

That should work with any JSON deserialization library.

Garr Godfrey
  • 8,257
  • 2
  • 25
  • 23
  • Hi I followed @Llama 's suggestions and I figured it out already, here is the code: `public class HistoryData //Innermost layer of the array { public string date_time { get; set; } public string url { get; set; } public string browser_source { get; set; } } public class BrowsingDataObject //Root JSON array { public string api_msg { get; set; } public string api_code { get;set; } public string api_status { get; set; } public List data { get; set; } }` Thank U so much Garr – jecs cor Sep 14 '21 at 03:42