-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; }
    }'
8
  • Please can you advise what issue you're facing? Commented Sep 14, 2021 at 3:04
  • hi @Llama I would like to know how to deserialize that kind of returned json nested array, I can't figure it out. Commented Sep 14, 2021 at 3: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. Commented Sep 14, 2021 at 3:09
  • JSON object ({ ... }) -> C# class. JSON array ([...]) -> C# collection type (list, array, ienumerable, etc.). Commented Sep 14, 2021 at 3:11
  • How can I set the name and number of arrays based from the JSON object in C# @Llama Thanks in advance. Commented Sep 14, 2021 at 3:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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<HistoryData> data { get; set; } } Thank U so much Garr

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.