Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6188539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:15:01+00:00 2026-05-24T02:15:01+00:00

I am new to using JSON and I am struggling to return specifically a

  • 0

I am new to using JSON and I am struggling to return specifically a list, within a list. I get the following JSON:

{
    "parameters": {
        "tbdb": "trudon",
        "min_prefix_length": "2",
        "service": "prefix",
        "template": "service.json",
        "term_prefix": "plu"},
    "termHints": [
        {
            "name": "Plumbers & Sanitary Engineers",
            "id":"209654",
            "values": {
                "value":"Plumbers & Sanitary Engineers",
                "pre_em":"",
                "em":"Plu",
                "post_em":"mbers & Sanitary Engineers",
                "nature":"PT",
                "id":"209654"
            }
        },
    ],
    "facets": [
        {
            "id":"209654",
            "name":"Plumbers & Sanitary Engineers"
        }
    ],
    "total":1
}

I have desrialized this using the the JSON DataContractJsonSerializer and it looks like the following:

edited: made the changes that were suggested by carlosfigueira here

    [DataContract]
    public class AutoCompleteResponse
    {
        [DataMember(Name = "parameters")]
        public Parameter Parameters { get; set; }
        [DataMember(Name = "termHints")]
        public List<termHints> hints { get; set; }
        [DataMember(Name = "total")]
        public string Total { get; set; }
    }
    [DataContract]
    public class Parameter
    {
        [DataMember(Name = "tbdb")]
        public string tbdb { get; set; }
        [DataMember(Name = "min_prefix_length")]
        public string min_prefix_length { get; set; }
        [DataMember(Name = "service")]
        public string service { get; set; }
        [DataMember(Name = "template")]
        public string template { get; set; }
        [DataMember(Name = "term_prefix")]
        public string term_prefrix { get; set; }
    }
    [DataContract]
    public class termHints
    {
        [DataMember(Name = "name")]
        public string Name { get; set; }
        [DataMember(Name = "id")]
        public string id { get; set; }
        [DataMember(Name = "values")]
        public values Values { get; set; }
        [DataMember(Name = "facets")]
        public facets Facets { get; set; }

    }
    [DataContract]
    public class values
    {
        [DataMember(Name = "value")]
        public string value_name { get; set; }
        [DataMember(Name = "pre_em")]
        public string pre_em { get; set; }
        [DataMember(Name = "em")]
        public string em { get; set; }
        [DataMember(Name = "post_em")]
        public string post_em { get; set; }
        [DataMember(Name = "nature")]
        public string nature { get; set; }
        [DataMember(Name = "id")]
        public string value_id { get; set; }
    }
    [DataContract]
    public class facets
    {
        [DataMember(Name = "id")]
        public string facet_id { get; set; }
        [DataMember (Name = "name")]
        public string facet_name {get; set; }
    }

The serialization I do looks like the following:

    WebClient w = new WebClient();
    w.DownloadStringCompleted += (a, b) =>
    {
        // Check for errors
        if (b == null) { return; }
        if (b.Error != null) { return; }
        if (string.IsNullOrEmpty(b.Result)) { return; }

        // Desearealize from JSON to .NET objects
        Byte[] bytes = Encoding.Unicode.GetBytes(b.Result);
        MemoryStream memoryStream = new MemoryStream(bytes);
        DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(AutoCompleteResponse));
        AutoCompleteResponse autocompleteSearchResponse = dataContractJsonSerializer.ReadObject(memoryStream) as AutoCompleteResponse;
        memoryStream.Close();

        // Raise Event
        this.OnSearchCompleted(
            new WhatEventArgs()
            {
                response = autocompleteSearchResponse
            }
        );
    };
    w.DownloadStringAsync(builder.Uri);

The return I create looks like the following:

    public class WhatEventArgs : EventArgs
    {
        public AutoCompleteResponse response { get; set; }
    }

*edit: adding the code that is used to get the data here incase this is helpful… This is how I am getting the data from the values, perhaps I am doing something wrong but I am not getting any data (it’s null) and I have no idea what I have done wrong *

    protected void cmdSearch_Click(object sender, EventArgs e)
    {
        AutocompleteWhat search = new AutocompleteWhat()
        {
            Num = 2
        };
        search.SearchCompleted += (a, b) =>
        {
            List<values> _value = new List<values>();
            foreach (termHints item in b.response.hints)
            {
                _value.Add(item.Values);
            }
            if (_value.Count > 0)
            {
                dgvResults.DataSource = _value;
            }
            else
            {
                dgvResults.DataSource = null;
            }
            dgvResults.DataBind();
        };
        search.Search("plu");
    }

I now need to get the actual values from hints, however the data returned from values is empty. I have no idea why, please see if you can see what I can not see.

edit Fixed the Json, was missing the closing ] for “facets”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:15:02+00:00Added an answer on May 24, 2026 at 2:15 am

    For the DataContractJsonSerializer to work you need to have the a type system with the schema which matches the JSON. The “values” field isn’t a list (or an array) – it’s another JSON object, so you don’t need to declare it as a List<Value> – it’s just a Value object there.

    Also, the DataMember Name properties on the termHints class are set to ‘Values’ and ‘Facets’ for their respective members, but in the json the names are in lower case, so you’ll need to change that as well (DataMember(Name = “values”) and DataMember(Name = “facets”).

    Update with code

    Another problem I see in the code / JSON is that in the JSON document, facets is a sibling of termHints, but in the data contracts you have, the facets are a child of the hints. So one of those needs to be changed. Below is a code with both versions, just pick one 🙂

    public class StackOverflow_6747339
    {
        class FacetsAsSiblingOfHints
        {
            [DataContract]
            public class AutoCompleteResponse
            {
                [DataMember(Name = "parameters")]
                public Parameter Parameters { get; set; }
                [DataMember(Name = "termHints")]
                public List<TermHints> Hints { get; set; }
                [DataMember(Name = "facets")]
                public List<Facets> Facets { get; set; }
                [DataMember(Name = "total")]
                public string Total { get; set; }
    
                public override string ToString()
                {
                    return string.Format("AutoCompleteResponse[Parameters={0},hints={1},facets={2},total={3}]",
                        Parameters, ListToString(Hints), ListToString(Facets), Total);
                }
            }
            [DataContract]
            public class Parameter
            {
                [DataMember(Name = "tbdb")]
                public string tbdb { get; set; }
                [DataMember(Name = "min_prefix_length")]
                public string min_prefix_length { get; set; }
                [DataMember(Name = "service")]
                public string service { get; set; }
                [DataMember(Name = "template")]
                public string template { get; set; }
                [DataMember(Name = "term_prefix")]
                public string term_prefrix { get; set; }
    
                public override string ToString()
                {
                    return string.Format("Parameter[tbdb={0},min_prefix_length={1},service={2},template={3},term_prefix={4}]",
                        tbdb, min_prefix_length, service, template, term_prefrix);
                }
            }
            [DataContract]
            public class TermHints
            {
                [DataMember(Name = "name")]
                public string Name { get; set; }
                [DataMember(Name = "id")]
                public string Id { get; set; }
                [DataMember(Name = "values")]
                public Values Values { get; set; }
    
                public override string ToString()
                {
                    return string.Format("TermHints[Name={0},Id={1},Values={2}]", Name, Id, Values);
                }
            }
            [DataContract]
            public class Values
            {
                [DataMember(Name = "value")]
                public string value_name { get; set; }
                [DataMember(Name = "pre_em")]
                public string pre_em { get; set; }
                [DataMember(Name = "em")]
                public string em { get; set; }
                [DataMember(Name = "post_em")]
                public string post_em { get; set; }
                [DataMember(Name = "nature")]
                public string nature { get; set; }
                [DataMember(Name = "id")]
                public string value_id { get; set; }
    
                public override string ToString()
                {
                    return string.Format("Values[value_name={0},pre_em={1},em={2},post_em={3},nature={4},value_id={5}]",
                        value_name, pre_em, em, post_em, nature, value_id);
                }
            }
            [DataContract]
            public class Facets
            {
                [DataMember(Name = "id")]
                public string facet_id { get; set; }
                [DataMember(Name = "name")]
                public string facet_name { get; set; }
    
                public override string ToString()
                {
                    return string.Format("Facets[facet_id={0},facet_name={1}]", facet_id, facet_name);
                }
            }
    
            const string json = @"{
    ""parameters"": {
        ""tbdb"": ""trudon"",
        ""min_prefix_length"": ""2"",
        ""service"": ""prefix"",
        ""template"": ""service.json"",
        ""term_prefix"": ""plu""},
    ""termHints"": [
        {
            ""name"": ""Plumbers & Sanitary Engineers"",
            ""id"":""209654"",
            ""values"": {
                ""value"":""Plumbers & Sanitary Engineers"",
                ""pre_em"":"""",
                ""em"":""Plu"",
                ""post_em"":""mbers & Sanitary Engineers"",
                ""nature"":""PT"",
                ""id"":""209654""
            }
        },
    ],
    ""facets"": [
        {
            ""id"":""209654"",
            ""name"":""Plumbers & Sanitary Engineers""
        }
    ],
    ""total"":1
    }";
    
            internal static void Test()
            {
                Console.WriteLine("Facets as siblings of the hints");
                DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(AutoCompleteResponse));
                MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
                AutoCompleteResponse obj = (AutoCompleteResponse)dcjs.ReadObject(ms);
                ms.Close();
                Console.WriteLine(obj);
                Console.WriteLine();
            }
        }
    
        class FacetsInsideHints
        {
            [DataContract]
            public class AutoCompleteResponse
            {
                [DataMember(Name = "parameters")]
                public Parameter Parameters { get; set; }
                [DataMember(Name = "termHints")]
                public List<TermHints> Hints { get; set; }
                [DataMember(Name = "total")]
                public string Total { get; set; }
    
                public override string ToString()
                {
                    return string.Format("AutoCompleteResponse[Parameters={0},hints={1},total={2}]",
                        Parameters, ListToString(Hints), Total);
                }
            }
            [DataContract]
            public class Parameter
            {
                [DataMember(Name = "tbdb")]
                public string tbdb { get; set; }
                [DataMember(Name = "min_prefix_length")]
                public string min_prefix_length { get; set; }
                [DataMember(Name = "service")]
                public string service { get; set; }
                [DataMember(Name = "template")]
                public string template { get; set; }
                [DataMember(Name = "term_prefix")]
                public string term_prefrix { get; set; }
    
                public override string ToString()
                {
                    return string.Format("Parameter[tbdb={0},min_prefix_length={1},service={2},template={3},term_prefix={4}]",
                        tbdb, min_prefix_length, service, template, term_prefrix);
                }
            }
            [DataContract]
            public class TermHints
            {
                [DataMember(Name = "name")]
                public string Name { get; set; }
                [DataMember(Name = "id")]
                public string Id { get; set; }
                [DataMember(Name = "values")]
                public Values Values { get; set; }
                [DataMember(Name = "facets")]
                public List<Facets> Facets { get; set; }
    
                public override string ToString()
                {
                    return string.Format("TermHints[Name={0},Id={1},Values={2},Facets={3}]", Name, Id, Values, ListToString(Facets));
                }
            }
            [DataContract]
            public class Values
            {
                [DataMember(Name = "value")]
                public string value_name { get; set; }
                [DataMember(Name = "pre_em")]
                public string pre_em { get; set; }
                [DataMember(Name = "em")]
                public string em { get; set; }
                [DataMember(Name = "post_em")]
                public string post_em { get; set; }
                [DataMember(Name = "nature")]
                public string nature { get; set; }
                [DataMember(Name = "id")]
                public string value_id { get; set; }
    
                public override string ToString()
                {
                    return string.Format("Values[value_name={0},pre_em={1},em={2},post_em={3},nature={4},value_id={5}]",
                        value_name, pre_em, em, post_em, nature, value_id);
                }
            }
            [DataContract]
            public class Facets
            {
                [DataMember(Name = "id")]
                public string facet_id { get; set; }
                [DataMember(Name = "name")]
                public string facet_name { get; set; }
    
                public override string ToString()
                {
                    return string.Format("Facets[facet_id={0},facet_name={1}]", facet_id, facet_name);
                }
            }
    
            const string json = @"{
    ""parameters"": {
        ""tbdb"": ""trudon"",
        ""min_prefix_length"": ""2"",
        ""service"": ""prefix"",
        ""template"": ""service.json"",
        ""term_prefix"": ""plu""},
    ""termHints"": [
        {
            ""name"": ""Plumbers & Sanitary Engineers"",
            ""id"":""209654"",
            ""values"": {
                ""value"":""Plumbers & Sanitary Engineers"",
                ""pre_em"":"""",
                ""em"":""Plu"",
                ""post_em"":""mbers & Sanitary Engineers"",
                ""nature"":""PT"",
                ""id"":""209654""
            },
            ""facets"": [
                {
                    ""id"":""209654"",
                    ""name"":""Plumbers & Sanitary Engineers""
                }
            ]
        },
    ],
    ""total"":1
    }";
    
            internal static void Test()
            {
                Console.WriteLine("Facets inside the hints");
                DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(AutoCompleteResponse));
                MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
                AutoCompleteResponse obj = (AutoCompleteResponse)dcjs.ReadObject(ms);
                ms.Close();
                Console.WriteLine(obj);
                Console.WriteLine();
            }
        }
    
        static string ListToString<T>(List<T> list)
        {
            if (list == null)
            {
                return "<<null>>";
            }
    
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            for (int i = 0; i < list.Count; i++)
            {
                if (i > 0) sb.Append(",");
                sb.Append(list[i]);
            }
    
            sb.Append("]");
            return sb.ToString();
        }
    
        public static void Test()
        {
            FacetsAsSiblingOfHints.Test();
            FacetsInsideHints.Test();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am new programmer.i am using Google Matrix API. i get the following response
when using new Date,I get something like follows: Fri May 29 2009 22:39:02 GMT+0800
I'm having issues gathering data using JSON on the Songkick API. I'm really new
Im using JSON.NET do deserlaize an object, but i cant get it to work
I'm fairly new to using JSON (as opposed to XML) and am currently working
*Hi , I am very new to using JSON in Jquery. I have a
I have some UTF8 encoding errors using JSON in JAVA: JSONObject json = new
Using JSON.Net, of the following 5 tests, the first and last pass while the
I'm new to JSON and really struggling with this. I've read countless other posts
im using json in my new app. im getting time in UNIX timestamps format.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.