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 8199389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:09:32+00:00 2026-06-07T06:09:32+00:00

EDIT: The problem with the below seems to be the bars label that is

  • 0

EDIT: The problem with the below seems to be the “bars” label that is put on the “items” array. I’ve tried to use the “JavaScriptSerializer” and the “NewtonSoft” tool and it errors out when that collection is in place.
ERROR: Cannot deserialize the current JSON object (e.g. {\”name\”:\”value\”}) into
type ‘System.Collections.Generic.List…

I’ve got a JSON return string from a call to a RESTful web service.
I’ve figured out how to turn this into a C# object.

Example JSON:

{
"property1": "value1",
"property2": "value2"
}

Example class that the JSON parses to:

public class FooClass
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}

I’m using the “JavaScriptSerializer” class in System.Web.Extensions namespace…works great!

Now, what about if the JSON has a collection item?
Like so:

{
"property1": "value1",
"property2": "value2",
"bars": {
"items": [
{
"sub_property1": "sub_value1",
"sub_property2": "sub_value2"
},
...
]
}
}

And I want to do something like this for my “FooClass” class:

public class FooClass
{
public string Property1 { get; set; }
public string Property2 { get; set; }
public List<BarClass> Bars { get; set; }
}

public class BarClass
{
public string Sub_Property1 { get; set; }
public string Sub_Property2 { get; set; }
}

Is there a way to tell the JavaScriptSerializer class that the “items” array equates to “Bars” so they can be “parsed in” to the class(es)?

Thanks for any advice you can give here.

  • 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-06-07T06:09:33+00:00Added an answer on June 7, 2026 at 6:09 am

    Sure — this behavior happens automatically. The important thing is that your BarClass have a parameterless constructor (I think this is common to serialization classes in general as well). The JavaScriptSerializer deserializing method takes in your type for use in reconstituting the object and goes through all subtypes in your class.

    From the documentation on MSDN: “During deserialization, the serializer’s current type resolver is referenced, which determines the managed type to use when converting elements that are nested inside arrays and dictionary types. As a result, the deserialization process iterates through all nested elements of input.”

    using System.Collections.Generic; 
    using System.Web.Script.Serialization;
    namespace ConsoleApplication1
    {
        class Program
        {
    
    
            private static void Main(string[] args)
            {
                var person = new Person();
                person.Name = "reacher gilt";
                person.Address = "100 East Way";
                person.Age = 74; 
                person.Aliases = new List<string>(new []{"teddy", "freddy", "eddy", "Betty"});
                person.Bars = new List<BarClass>(new[]{ 
                    new BarClass("beep","boop"),
                    new BarClass("meep","moop"),
                    new BarClass("feep","foop"),
                });
                JavaScriptSerializer serializer = new JavaScriptSerializer();
    
                string jsonString = serializer.Serialize(person);
    
                var rehydrated = serializer.Deserialize<Person>(jsonString);
            }
    
        }
    
    
        class Person
        {
            public string Name { get; set; }
    
            public string Address { get; set; }
    
            public int Age { get; set; }
    
            public List<string> Aliases;
            public List<BarClass> Bars { get; set; }
        }
        class BarClass
        { 
            public string Sub_Property1 { get; set; }
            public string Sub_Property2 { get; set; }
            public BarClass() { }
            public BarClass (string one, string two)
            {
                Sub_Property1 = one;
                Sub_Property2 = two;
            }
        }
    }
    

    edit: After coach rob’s clarifying comment:

    using System.Collections.Generic;
    using System.Web.Script.Serialization;
    namespace ConsoleApplication1
    {
        class Program
        {
    
    
            private static void Main(string[] args)
            {
                var person = new Person();
                person.Name = "reacher gilt";
                person.Address = "100 East Way";
                person.Age = 74; 
                person.Aliases = new List<string>(new []{"teddy", "freddy", "eddy", "Betty"});
                person.Bars = new Dictionary<string, List<BarClass>>();
    
                person.Bars.Add("items",
                    new List<BarClass>(new[]{ 
                    new BarClass("beep","boop"),
                    new BarClass("meep","moop"),
                    new BarClass("feep","foop"),
                }));
                JavaScriptSerializer serializer = new JavaScriptSerializer();
    
                string jsonString = serializer.Serialize(person);
    
                var rehydrated = serializer.Deserialize<Person>(jsonString);
            }
    
        }
    
    
        class BarClass
        { 
            public string Sub_Property1 { get; set; }
            public string Sub_Property2 { get; set; }
            public BarClass() { }
            public BarClass (string one, string two)
            {
                Sub_Property1 = one;
                Sub_Property2 = two;
            }
        }
    
        class Person
        {
            public string Name { get; set; }
    
            public string Address { get; set; }
    
            public int Age { get; set; }
    
            public List<string> Aliases;
            public Dictionary <string, List<BarClass> >Bars { get; set; }
        }
    }
    

    which gets you json like :

    {
        "Aliases":
        [
            "teddy",
            "freddy",
            "eddy",
            "Betty"
        ],
        "Name":"reacher gilt",
        "Address":"100 East Way",
        "Age":74,
        "Bars":
        {
            "items":
            [
                {
                    "Sub_Property1":"beep",
                    "Sub_Property2":"boop"
                },
    
                {
                    "Sub_Property1":"meep",
                    "Sub_Property2":"moop"
                },
    
                {
                    "Sub_Property1":"feep",
                    "Sub_Property2":"foop"
                }
            ]
        }
    }
    

    I also see people using the DataContractJsonSerializer, which is indeed one way to serialize to json, but System.Web.Script.Serialization.JavaScriptSerializer was specified in the question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[EDIT: Problem solved. Please see my answer below.] In my app I call the
EDIT: This problem has been solved. See below. Hey all. I'm building an iPhone
Ok, I'll put the code first hopefully to make it clearer. ***Edit: Problem is
I've tried to various solutions to this problem, but nothing seems to be working.
I have an array, example below, that I am serializing into the database and
SECOND EDIT It seems that upon loading the page, not when submitting, that the
Below is the code for my autocomplete. The problem is that it mostly works.
EDIT: Changed example below to one that actually demonstrates the SIOF. I am trying
Big problem. I'm not sure what it was that I did, but it seems
It seems to be a bug or problem when I use PHP PDO fetchOject

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.