Aereal
Aereal

Reputation: 103

Deserializing a list of values from JSON using C#

I have two classes that make use of auto properties:

class B
{
    int a { get; set; }
    int b { get; set; }
    List <A> listofa { get; set; }
}

class A
{
    string c { get; set; }
    string d { get; set; }
}

I have deserialized a JSON data source and imported data into the classes. Actually I have 3 class A objects in my JSON and one B. So how can I get the values of my objects that were created?

I can get the values of class B properties by testOfB.a (after the deserialization of course):

B testOfB = JsonConvert.DeserializeObject<RootObject>(testjson);

But how can I get the value of testOfB.listofa? I am sure I should use a foreach loop. (I found some examples in some other posts.) But none of it is working for me. I tried something like this:

foreach(object[] in A)
{
    ???
}

Am I close? Any help would be really appreciated!!

EDIT

Here is the JSON I am deserializing:

{
    "a": 12,
    "b": 13,
    "A": [
        {
            "c": "test",
            "d": "test"
        },
        {
            "c": "test2",
            "d": "test2"
        },
        {
            "c": "‌​test3",
            "d": "test3"
        }
    ]
}

Upvotes: 1

Views: 84

Answers (2)

Brian Rogers
Brian Rogers

Reputation: 129827

You have a few issues here:

  1. Your member properties need to be public
  2. In your JSON the list property is called A but in your class it's call listofa. You either need to decorate your listofa property with [JsonProperty("A")] or rename listofa to A so that it matches the JSON).
  3. You are deserializing to a RootObject class which you haven't defined.
  4. Your foreach loop isn't right, as @billbo pointed out.

Here is corrected code that works:

class B
{
    public int a { get; set; }
    public int b { get; set; }
    [JsonProperty("A")]
    public List<A> listOfA { get; set; }
}

class A
{
    public string c { get; set; }
    public string d { get; set; }
}

class Program
{
    public static void Main(string[] args)
    {
        string json = @"
        {
            ""a"": 12,
            ""b"": 13,
            ""A"": [
                {
                    ""c"": ""test"",
                    ""d"": ""test""
                },
                {
                    ""c"": ""test2"",
                    ""d"": ""test2""
                },
                {
                    ""c"": ""test3"",
                    ""d"": ""test3""
                }
            ]
        }";

        B testOfB = JsonConvert.DeserializeObject<B>(json);

        Console.WriteLine("a: " + testOfB.a);
        Console.WriteLine("b: " + testOfB.b);
        foreach (A a in testOfB.listOfA)
        {
            Console.WriteLine("c: " + a.c);
            Console.WriteLine("d: " + a.d);
        }
    }
}

Output:

a: 12
b: 13
c: test
d: test
c: test2
d: test2
c: test3
d: test3

Upvotes: 1

billbo
billbo

Reputation: 41

Your foreach statement doesn't look correct. I would write it like this:

foreach(A a in test.listofa)
{
    string c = a.c;
    string d = a.d;
}

Upvotes: 1

Related Questions