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"
    }
]
}
mattjankowskiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
If you want to have simple and meaningful access to that data, then you would need to create a class (or classes) that represents that structure.

e.g.

public class TheClass
{
    public string Message { get; set; }
    public FieldInfo[] Array { get; set; }
}

public class FieldInfo
{
    public string Field0 { get; set; }
    public string Field1 { get; set; }
}

Open in new window


Then it is simply a matter of calling the Newtonsoft deserializer:

[HttpPost]
public ActionResult Index(string json)
{
    // Pretend that this came in from the parameter
    json = @"{
                    ""Message"": null,
                    ""Array"": [
                        {
                            ""field0"": ""world"",
                            ""field1"": ""hello""
                        },
                        {
                            ""field0"": ""data1"",
                            ""field1"": ""data2""
                        }
                    ]
                }";

    TheClass instance = (TheClass)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(TheClass));

    return View();
}

Open in new window


If you were hoping to avoid having to create a new class, then you could use the deserializer without passing in specific type information along with Reflection, but that could get a bit messy (and slow[er]), and I think it would end up being about the same amount of work.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
leakim971PluritechnicianCommented:
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

0
käµfm³d 👽Commented:
@leakim971

How exactly is your example different than mine?
0
leakim971PluritechnicianCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.