Link to home
Start Free TrialLog in
Avatar of mattjankowski
mattjankowski

asked on

How to Handle a nest JSON array in .NET

How would I extract "Array" from the JSON object below using .NET? I can parse the string, but I would prefer a more elegant solution. I am using the Newtonsoft library (jObject, JArray).

Thanks,
Matt

{
  "Message": null,
  "Array": [
    {
      "field0": "world",
      "field1": "hello"
    },
    {
      "field0": "data1",
      "field1": "data2"
    }
]
}
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft;

namespace JSONUnserialize
{
    class Program
    {
        static void Main(string[] args)
        {
            String blabla = "{\"Message\":null,\"Array\":[{\"field0\":\"world\",\"field1\":\"hello\"},{\"field0\":\"data1\",\"field1\":\"data2\"}]}";
            Blabla b = (Blabla)Newtonsoft.Json.JsonConvert.DeserializeObject(blabla, typeof(Blabla));

            String item1_field0 = (b.Array[0].field0);
            String item1_field1 = (b.Array[0].field1);

            String item2_field0 = (b.Array[1].field0);
            String item2_field1 = (b.Array[1].field1);
        }

        public class Blabla
        {
            public string Message { get; set; }
            public List<Blablabla> Array { get; set; }
        }

        public class Blablabla
        {
            public string field0 { get; set; }
            public string field1 { get; set; }
        }
    }
}

Open in new window

@leakim971

How exactly is your example different than mine?
Did not saw your before posting mine
You click on the link in the email
It open the page
You open Visual Studio
You code
You test
You copy/paste in the window

@mattjankowski you can award all the point to @kaufmed, no worry