-2

I have below complex JSON format and I want to create C# class from that Can any one help to sort it out the class and hierarchy of the classes.

{
'scriptVersion': 1,
  'pagesData': [
    {
      'state': 'ABC',
      'idNo': '55',
      'noOfRecords': {
        '10': {
          'key': 'key1',
          'value': 'value1'
        },
        '201': {
          'key': 'key2',
          'value': 'value2'
        },
        '300': {
          'key': 'key3',
          'value': 'value3'
        }
        }
    }]
}
Bhavik
  • 177
  • 1
  • 5
  • What have you done so far? – Cid Jul 12 '18 at 09:54
  • I try to create class and sub class for the nested part but confusion and query is related to 'noOfRecords' section that how maintain that as values are changing for 10,201,300 – Bhavik Jul 12 '18 at 12:38

2 Answers2

1

Copy the json string

Paste in VS via [ Edit | Paste Special | Paste Json as Classes ]

The are also multiple online json to C# generator tools

RazorShorts
  • 115
  • 5
  • Yes, That I know to create JSON class but if you noticed under "noOfRecords" value of "10","201" will be dynamic so confusion is how to create property for that – Bhavik Jul 12 '18 at 11:02
  • Well it is kinda a weird json i expected to have a integer on a property 'noOfRecords'. This json is implying the noOfRecords property has 3 complex properties named .10,201,300. Should these be in a array of some sort. Ask the guys who gave you this json if the have a documentation for it, i've got a gut feeling telling me this json isn't finished. – RazorShorts Jul 12 '18 at 11:18
  • Ok, So we can have n no of that types of records under noOfRecords so will not able to create common class for that? – Bhavik Jul 12 '18 at 11:20
0

base on your json code this is the class

public class c10
{
    public string key { get; set; }
    public string value { get; set; }
}

public class c201
{
    public string key { get; set; }
    public string value { get; set; }
}

public class c300
{
    public string key { get; set; }
    public string value { get; set; }
}

public class NoOfRecords
{
    public c10 { get; set; }
    public c201 { get; set; }
    public c300 { get; set; }
}

public class PagesData
{
    public string state { get; set; }
    public string idNo { get; set; }
    public NoOfRecords noOfRecords { get; set; }
}

public class RootObject
{
    public int scriptVersion { get; set; }
    public List<PagesData> pagesData { get; set; }
}
Josue Barrios
  • 440
  • 5
  • 10