0

I'm having trouble unpacking a JSON response to a C# structure.

The JSON data comes from a call to Etsy to get a seller's list of taxonomies (.../taxonomy/seller/get). A sample (fudged) JSON response is:

"{\"count\":1,\"results\":[{\"id\":1,\"name\":\"Accessories\",\"children_ids\":[1728],\"path\":\"accessories\",\"children\":[ {\"id\":1728,\"name\":\"Baby Accessories\",\"children_ids\":[135,6078],\"path\":\"accessories.baby_accessories\",\"children\":[{\"id\":135,\"name\":\"Baby Carriers & Wraps\",\"children_ids\":[],\"path\":\"accessories.baby_accessories.baby_carriers_and_wraps\",\"children\":[],\"level\":2,\"parent\":\"accessories.baby_accessories\",\"parent_id\":1728,\"short_name\":null,\"all_name\":null,\"description\":null,\"page_title\":null,\"nav\":null,\"category_id\":69150455,\"full_path_taxonomy_ids\":[1,1728,135],\"source_finder\":\"seller:01-2017\",\"attributeValueSets\":[],\"filters\":{\"buyer\":[],\"byCraft\":[]},\"version\":\"3ff712c\"},{\"id\":6078,\"name\":\"Children's Photo Props\",\"children_ids\":[],\"path\":\"accessories.baby_accessories.childrens_photo_props\",\"children\":[],\"level\":2,\"parent\":\"accessories.baby_accessories\",\"parent_id\":1728,\"short_name\":null,\"all_name\":null,\"description\":null,\"page_title\":null,\"nav\":{\"buyer_target\":null,\"is_vintage\":null},\"category_id\":69150467,\"full_path_taxonomy_ids\":[1,1728,6078],\"source_finder\":\"seller:01-2017\",\"attributeValueSets\":[{\"attribute\":3,\"possibleValues\":[],\"selectedValues\":[],\"isRequired\":false,\"displayName\":\"Occasion\",\"novaIds\":[],\"version\":\"3ff712c\",\"taxonomyNode\":6078,\"userInputValidator\":null}],\"filters\":{\"buyer\":[],\"byCraft\":[]},\"version\":\"3ff712c\"}],\"level\":1,\"parent\":\"accessories\",\"parent_id\":1,\"short_name\":null,\"all_name\":null,\"description\":null,\"page_title\":null,\"nav\":null,\"category_id\":69150467,\"full_path_taxonomy_ids\":[1,1728],\"source_finder\":\"seller:01-2017\",\"attributeValueSets\":[{\"attribute\":2,\"possibleValues\":[],\"selectedValues\":[],\"isRequired\":false,\"displayName\":\"Primary color\",\"novaIds\":[],\"version\":\"3ff712c\",\"taxonomyNode\":1728,\"userInputValidator\":null}],\"filters\":{\"buyer\":[],\"byCraft\":[]},\"version\":\"3ff712c\"}]}]}"

I've pasted the JSON to https://jsonformatter.curiousconcept.com/ and viewed the formatted response, and as far as I can tell the way I've structured my classes for the response is ok (but clearly, it's not).

My confusion is that "children" in the JSON response seems to me like a recursive class, and so I created a class called children, which in itself can have "children" in its response.

But when I run the code, it almost works (please forgive any inefficiencies that you spot in the classes), but fails to unpack "children" - zero records unpacked.

Zero children records

My code:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;


namespace Etsy
{
    public class TaxonomyData
    {
        #region Classes

        #region Supporting classes

        public class Nav
        {
            public string buyer_target { get; set; }
            public string is_vintage { get; set; }
        }

        public class ByCraft
        {
        }

        public class Buyer
        {
        }

        public class Filters
        {
            public Buyer buyer { get; set; }
            public ByCraft byCraft { get; set; }
        }

        public class AttributeValueSets
        {
            public int attribute { get; set; }
            public List<int> possibleValues { get; set; }
            public List<int> selectedValues { get; set; }
            public string isRequired { get; set; }
            public string displayName { get; set; }
            public List<string> novaId { get; set; }
            public string version { get; set; }
            public int taxonomyNode { get; set; }
            public string userInputValidator { get; set; }

            public AttributeValueSets()
            {
                possibleValues = new List<int>();
                selectedValues = new List<int>();
                novaId = new List<string>();
            }
        }

        public class Children
        {
            public string id { get; set; }
            public string name { get; set; }
            public List<int> children_ids { get; set; }
            public string path { get; set; }
            public List<Children> children { get; set; }

            public string level { get; set; }
            public string parent { get; set; }
            public int parent_id { get; set; }
            public string short_name { get; set; }
            public string all_name { get; set; }
            public string description { get; set; }
            public string page_title { get; set; }
            public Nav nav { get; set; }
            public int category_id { get; set; }
            public List<int> full_path_taxonomy_ids { get; set; }
            public string source_finder { get; set; }
            public List<AttributeValueSets> attributeValueSets { get; set; }
            public Filters filters { get; set; }
            public string version { get; set; }

            public Children()
            {
                children_ids = new List<int>();
                children = new List<Children>();

                nav = new Nav();
                full_path_taxonomy_ids = new List<int>();
                attributeValueSets = new List<AttributeValueSets>();
                filters = new Filters();
            }
        }

        public class Results
        {
            public string id { get; set; }
            public string name { get; set; }
            public List<int> children_ids { get; set; }
            public string path { get; set; }
            List<Children> children { get; set; }

            public Results()
            {
                children_ids = new List<int>();
                children = new List<Children>();
            }
        }

        #endregion Supporting classes

        public class TaxonomyResponse
        {
            public int count { get; set; }
            public List<Results> results { get; set; }

            public TaxonomyResponse()
            {
                results = new List<Results>();
            }
        }

        #endregion Classes

        public void GetTaxonomyData()
        {
            string testData = "{\"count\":1,\"results\":[{\"id\":1,\"name\":\"Accessories\",\"children_ids\":[1728],\"path\":\"accessories\",\"children\":[ {\"id\":1728,\"name\":\"Baby Accessories\",\"children_ids\":[135,6078],\"path\":\"accessories.baby_accessories\",\"children\":[{\"id\":135,\"name\":\"Baby Carriers & Wraps\",\"children_ids\":[],\"path\":\"accessories.baby_accessories.baby_carriers_and_wraps\",\"children\":[],\"level\":2,\"parent\":\"accessories.baby_accessories\",\"parent_id\":1728,\"short_name\":null,\"all_name\":null,\"description\":null,\"page_title\":null,\"nav\":null,\"category_id\":69150455,\"full_path_taxonomy_ids\":[1,1728,135],\"source_finder\":\"seller:01-2017\",\"attributeValueSets\":[],\"filters\":{\"buyer\":[],\"byCraft\":[]},\"version\":\"3ff712c\"},{\"id\":6078,\"name\":\"Children's Photo Props\",\"children_ids\":[],\"path\":\"accessories.baby_accessories.childrens_photo_props\",\"children\":[],\"level\":2,\"parent\":\"accessories.baby_accessories\",\"parent_id\":1728,\"short_name\":null,\"all_name\":null,\"description\":null,\"page_title\":null,\"nav\":{\"buyer_target\":null,\"is_vintage\":null},\"category_id\":69150467,\"full_path_taxonomy_ids\":[1,1728,6078],\"source_finder\":\"seller:01-2017\",\"attributeValueSets\":[{\"attribute\":3,\"possibleValues\":[],\"selectedValues\":[],\"isRequired\":false,\"displayName\":\"Occasion\",\"novaIds\":[],\"version\":\"3ff712c\",\"taxonomyNode\":6078,\"userInputValidator\":null}],\"filters\":{\"buyer\":[],\"byCraft\":[]},\"version\":\"3ff712c\"}],\"level\":1,\"parent\":\"accessories\",\"parent_id\":1,\"short_name\":null,\"all_name\":null,\"description\":null,\"page_title\":null,\"nav\":null,\"category_id\":69150467,\"full_path_taxonomy_ids\":[1,1728],\"source_finder\":\"seller:01-2017\",\"attributeValueSets\":[{\"attribute\":2,\"possibleValues\":[],\"selectedValues\":[],\"isRequired\":false,\"displayName\":\"Primary color\",\"novaIds\":[],\"version\":\"3ff712c\",\"taxonomyNode\":1728,\"userInputValidator\":null}],\"filters\":{\"buyer\":[],\"byCraft\":[]},\"version\":\"3ff712c\"}]}]}";
            TaxonomyResponse response;

            try
            {
                response = JsonConvert.DeserializeObject<TaxonomyResponse>(testData);

#if DEBUG
                if (!string.IsNullOrEmpty(testData))
                {
                    //
                    // And the response is ZERO children unpacked.
                    //
                }
#endif          
            }
            catch (Exception ex)
            {
            }
        }

    }
}

What am I doing? Why isn't children unpacking?

err1
  • 499
  • 9
  • 22
  • 2
    Just curious, have you tried this? http://json2csharp.com/ It'll generate classes for you based on a json sample. I've used it a handful of times and worked well enough for my purposes. – jleach Aug 29 '17 at 12:48
  • 1
    THANK YOU BOTH! @CodeCaster's response was perfect as was yours and solved my problem. Code was almost there :D – err1 Aug 29 '17 at 13:14

0 Answers0