-5

Could someone help me in structuring the class for below format JSON.

I have already tried http://json2csharp.com/ tool. It did not work as my list of people are dynamic, i.e. values 123, 124 etc are not pre-defined.

{
  "people":
     {
        "123":"jack henry",
        "124":"john henry",
        "125":"jill henry",
        "215":"jim henry",
        ...
     }
}
Arghya C
  • 9,805
  • 2
  • 47
  • 66
Dheeraj Palagiri
  • 1,829
  • 3
  • 23
  • 46
  • 2
    http://json2csharp.com/ – Nasreddine Nov 06 '15 at 17:08
  • 4
    Possible duplicate of [How to Convert JSON object to Custom C# object?](http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object) – MethodMan Nov 06 '15 at 17:10
  • I see a duplicate entry in your names. You listed the name "124" twice with 2 different values. You may have accidentally misformatted this. `{"people":[ {"ID":"123", "Name":"jack henry"}, {"ID":"124", "Name":"john henry"}, {additional objects} ]}` – Grungondola Nov 06 '15 at 17:13
  • This is not a duplicate of another question mentioned, the list is dynamic here... – Dheeraj Palagiri Nov 06 '15 at 17:33
  • @Nasreddine i have already tried that before posting a question here.. that did not help me.. – Dheeraj Palagiri Nov 06 '15 at 17:39

3 Answers3

2

Visual Studio > Edit > Paste Special > Paste JSON as classes

public class Rootobject
{
    public People people { get; set; }
}

public class People
{
    public string _123 { get; set; }
    public string _124 { get; set; }
    public string _125 { get; set; }
    public string _215 { get; set; }
}

You already got an answer for your question. But, looking at the sample JSON looks like you are actually storing a list of persons. If that is the case, you might create classes like this

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class People
{
    public List<Person> Persons { get; set; }
    //other properties
}

And have your JSON standardized as

{
  "Persons": [
    {
      "Id": 123,
      "Name": "Jack"
    },
    {
      "Id": 124,
      "Name": "John"
    }
  ]
}

Which will be much more meaningful and readable (by code and human).

Arghya C
  • 9,805
  • 2
  • 47
  • 66
  • that's not what i want... 123, 124, 125, 215 they are not static... how we can create a class properties dynamically..... – Dheeraj Palagiri Nov 06 '15 at 17:12
  • 1
    Well, where is that mentioned in your question? – Arghya C Nov 06 '15 at 17:16
  • No problem. Good that you already got answer for your question. Maybe you can revisit the problem and see if you really need that particular JSON or your problem can be solved in some other way. – Arghya C Nov 06 '15 at 17:44
  • @DheerajPalagiri I have just updated my answer with some suggestion. Please have a look if that helps you in any way, else just ignore :) – Arghya C Nov 06 '15 at 17:57
2
public class Root
{
     public Dictionary<string, string> people = new Dictionary<string,string>();
}

Using Json.NET:

Root root = new Root();

root.people.Add("123", "jack henry");
//... Add more people

string json = JsonConvert.SerializeObject(root);
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
0

Source: http://json2csharp.com/

public class People
{
    public string __invalid_name__123 { get; set; }
    public string __invalid_name__124 { get; set; }
    public string __invalid_name__125 { get; set; }
    public string __invalid_name__215 { get; set; }
}

public class RootObject
{
    public People people { get; set; }
}
John Paul
  • 827
  • 2
  • 6
  • 16