3

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.

coach_rob
  • 877
  • 13
  • 28
  • see http://stackoverflow.com/questions/1100191/javascriptserializer-deserialize-how-to-change-field-names – Jimmy Jul 10 '12 at 22:17

2 Answers2

1

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.

Reacher Gilt
  • 1,813
  • 12
  • 26
  • Please see the edit above. This approach doesn't work given that the "items" array is nested below the "bars" property. Any ideas? – coach_rob Jul 11 '12 at 02:44
  • Well, look : there's a difference between the json and your class. The json has an extra data structure between your Bars and the actual list. I've edited the answer to change the class to match the json, but you'll have to make the call on whether this is the "correct" data structure for you. – Reacher Gilt Jul 11 '12 at 15:31
  • With a small change from: public List Bars { get; set; } to public Dictionary> Bars { get; set;} this worked. While the data structure isn't ideal, it's certainly more manageable (i.e. maintaining custom JSON parsing) than anything else I've found. THANK YOU for your assistance! – coach_rob Jul 11 '12 at 16:41
0

Try this:

[DataContract]
public class FooClass
{
    [DataMember]
    public string Property1 { get; set; }

    [DataMember]
    public string Property2 { get; set; }

    [DataMember]
    public Bars Bars { get; set; }
}

[DataContract]
public class Bars
{
    [DataMember]
    public List<Item> Items { get; set; }
}

[DataContract]
public class Item
{
    [DataMember]
    public string Sub_Property1 { get; set; }

    [DataMember]
    public string Sub_Property2 { get; set; }
}
timothyclifford
  • 6,799
  • 7
  • 57
  • 85