0

I am facing a problem for parsing a JSON array in C#

{
    "details" : [{
            "state" : "myState1",
            "place" : [{
                    "name" : "placeName",
                    "age" : 13
                }
            ]
        }, {
            "state" : "myState2",
            "place" : [{
                    "name1" : "placeName"
                }
            ]
        }, {
            "state" : "myState3",
            "place" : [{
                    "name2" : "placeName"
                }
            ]
        }
    ]
}

My code is:

static void Main(string[] args) 
{ 
    string txt = File.ReadAllText("MyJSONFile.txt"); 
    JavaScriptSerializer ser = new JavaScriptSerializer(); 
    var data = ser.Deserialize(txt); 
}

public class Wrap
{ 
    public List<Dictionary<string, object>> details { get; set; } 
}

How can I read data from these dictionaries? Sometimes the JSON will include only 1 facility's details, but other times there are more than 30 items in the array. This data is being pulled from the database.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
Hope
  • 1,252
  • 4
  • 17
  • 35
  • public class Wrap { public List> details { get; set; } } – Hope May 21 '12 at 10:26
  • 2
    check this [link](http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/using-c-4.0-and-dynamic-to-parse-json.aspx) or this [link](http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object/3806407#3806407) – Darshana May 21 '12 at 10:59
  • I will suggest you to use [Newtonsoft.Json](https://www.nuget.org/packages/Newtonsoft.Json/) and for generating the required classes you can use [Json2CSharp](http://json2csharp.com/) – Rohit Vipin Mathews Oct 27 '15 at 07:17

2 Answers2

0

You may use the following C# classes structure:

public class Place
{
    public string name { get; set; }
    public int age { get; set; }
    public string name1 { get; set; }
    public string name2 { get; set; }
}

public class Detail
{
    public string state { get; set; }
    public List<Place> place { get; set; }
}

public class Root
{
    public List<Detail> details { get; set; }
}

public class Program 
{
    static void Main(string[] args) 
    { 
        string txt = File.ReadAllText("MyJSONFile.txt"); 
        JavaScriptSerializer ser = new JavaScriptSerializer(); 
        var data = ser.Deserialize<Root>(txt); 

        Console.WriteLine(data.details.Count); // 3
        Console.WriteLine(data.details[0].state); // myState1
        Console.WriteLine(data.details[1].place.Count); // 1
        Console.WriteLine(data.details[1].place[0].age); // 13
    }
}
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
0

The class structure you are using is wrong. You will have to use the structure as follows, which corresponds to your JSON. This structure have been generated using json2csharp

public class Place
{
    public string name { get; set; }
    public int age { get; set; }
    public string name1 { get; set; }
    public string name2 { get; set; }
}

public class Detail
{
    public string state { get; set; }
    public List<Place> place { get; set; }
}

public class RootObject
{
    public List<Detail> details { get; set; }
}

Now in your code you can de serialize this using Newtonsoft.Json as follows:

static void Main(string[] args)
{
     string jsonText = System.IO.File.ReadAllText("MyJSONFile.txt");
     var rootObj = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonText);
     rootObj.details.ForEach(detail =>
     {
         Console.WriteLine(detail.state);
         detail.place.ForEach(p =>
         {
             if (string.IsNullOrWhiteSpace(p.name) == false)
             {
                  Console.WriteLine(p.name);
             }
             if (string.IsNullOrWhiteSpace(p.name1) == false)
             {
                  Console.WriteLine(p.name1);
             }
             if (string.IsNullOrWhiteSpace(p.name2) == false)
             {
                  Console.WriteLine(p.name2);
             }
             if (p.age > 0)
             {
                  Console.WriteLine(p.age);
             }
             Console.WriteLine(string.Empty);
         });
     });
     Console.ReadKey(true);
}
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112