-2

I have following JSON and I want to deserialize using class. I have read few reference but it doesn't work in this case.

I want to create class which can deserialize following JSON and work fine. Can anybody please suggest me?

JSON:

{
   "panels":{
      "LBL_EDITVIEW_PANEL1":{
         "lable_value":"New Panel 1",
         "rows":[
            [
               {
                  "name":"subject",
                  "label_value":"Subject",
                  "label":"subject",
                  "type":"String",
                  "required":"true",
                  "CanBeSecuredForCreate":"false",
                  "CanBeSecuredForRead":"false",
                  "CanBeSecuredForUpdate":"false"
               },
               {
                  "name":"scheduledstart",
                  "label_value":"Start Time",
                  "label":"scheduledstart",
                  "type":"DateTime",
                  "required":"true",
                  "CanBeSecuredForCreate":"false",
                  "CanBeSecuredForRead":"false",
                  "CanBeSecuredForUpdate":"false"
               }
            ]
         ]
      }
   }
} 

If there are 3 panels, above JSON has LBL_EDITVIEW_PANEL1, LBL_EDITVIEW_PANEL2, LBL_EDITVIEW_PANEL3. It makes really complications.

I am asking how to do it programmatically.

croxy
  • 4,082
  • 9
  • 28
  • 46
Nanji Mange
  • 2,155
  • 4
  • 29
  • 63
  • 1
    Possible duplicate of [Deserialize JSON with C#](http://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) – Liam Nov 11 '16 at 13:47

3 Answers3

1

So in your json, the "panels" object will have 0 to n "LBL_EDITVIEW_PANEL{number}" properties, where n is unbounded. This implies that the panels object is behaving like a hash table.

This should work. IDictionary keys will get serialized into essentially dynamic properties in your JSON.

public class Rootobject
{
    public System.Collections.Generic.IDictionary<string, panel> panels { get; set; }
}

public class panel
{
    public string label_value { get; set; }
    public row[] rows { get; set }
}

public class row
{
    public string name { get; set; }
    public string label_value { get; set; }
    public string label { get; set; }
    public string type { get; set; }
    public string required { get; set; }
    public string CanBeSecuredForCreate { get; set; }
    public string CanBeSecuredForRead { get; set; }
    public string CanBeSecuredForUpdate { get; set; }
}

UPDATE

An example usage with JSON.NET

using LightInject;
using System;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = new Rootobject();
            x.panels = new System.Collections.Generic.Dictionary<string, panel>();
            x.panels.Add("LBL_EDITVIEW_PANEL1", new panel()
            {
                label_value = "",
                rows = new row[]
                {
                    new row
                    {
                        name = "subject",
                        label_value = "Subject",
                        label = "subject",
                        type = "String",
                        required = "true",
                        CanBeSecuredForCreate = "false",
                        CanBeSecuredForRead = "false",
                        CanBeSecuredForUpdate = "false",
                    },
                    new row
                    {
                        name = "scheduledstart",
                        label_value = "Start Time",
                        label = "scheduledstart",
                        type = "DateTime",
                        required = "true",
                        CanBeSecuredForCreate = "false",
                        CanBeSecuredForRead = "false",
                        CanBeSecuredForUpdate = "false",
                    },
                },
            });

            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(x));

            Console.ReadLine();
        }
    }
}
Jeffrey Patterson
  • 2,342
  • 1
  • 13
  • 9
0

There's an option in Visual Studio:

  • Edit > Paste Special > Paste JSON as Classes
kenorb
  • 155,785
  • 88
  • 678
  • 743
Miguel
  • 150
  • 7
0

I will try to help. It seems that you ant to get a c# object from json string. I will show simpler example for deserializing json object in Visual Studio. Right click on your project and search for Manage Nu Get Packages. When window opens search for Newtonsoft package. Now for your main program.

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public DeserializeObjectClass()
{
    static void Main(string[] args)
    {
        // Code for getting json object. 
        string jsonObject = "{'panels': {'address": 'random', 'id': '0'}}";
        panels myPanel = JsonConvert.DeserializeObject<panels>(jsonObject);

        Console.WriteLine(panel.adress);
    }
}

// And now for you panel object in c#
public class panels
{
     public panels()
     {
     }

     public string address { get; set; }

     public string id { get; set; }

}

I'm showing this just as example it may have some warnings or minor errors becouse I've not tested it yet.

Luke359
  • 447
  • 6
  • 14