0

I am trying to create a List of a class with this object:

{
  "801": {
    "Name": "SomeWhere",
    "Lanes": {
      "2": {
        "Ip": "172.23.101.21"
      },
      "4": {
        "Ip": "172.23.101.41"
      },
      "6": {
        "Ip": "172.23.101.61"
      },
      "8": {
        "Ip": "172.23.101.81"
      },
      "9": {
        "Ip": "172.23.101.91"
      },
      "11": {
        "Ip": "172.23.101.111"
      }
    }
  },
  "803": {
    "Name": "OverThere",
    "Lanes": {
      "3": {
        "Ip": "172.23.103.31"
      },
      "6": {
        "Ip": "172.23.103.61"
      }
    }
  }
}

I need it to be able to use it like this: List<ClassOfJson> jsonData = new List<ClassOfJson>();

I am using Newtonsoft in C#. What can I do to resolve this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Ibanez1408
  • 4,550
  • 10
  • 59
  • 110
  • You can use [json2csharp.com](http://json2csharp.com/) to generate c# classes according to json structure – Ramin Rahimzada Aug 05 '19 at 06:28
  • Possibly a duplicate of [How to auto-generate a C# class file from a JSON object string](https://stackoverflow.com/q/21611674/3744182). Possibly [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182) or [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/a/34213724/3744182) also apply. – dbc Aug 05 '19 at 06:30
  • https://app.quicktype.io/#l=cs&r=json2csharp (linked to from [How to auto-generate a C# class file from a JSON object string](https://stackoverflow.com/q/21611674/3744182)) generates the correct data model. Do you need that as an answer? – dbc Aug 05 '19 at 06:47
  • The root object isn't a list though, it's a dictionary. Why do you need it to be a list? – dbc Aug 05 '19 at 07:02

1 Answers1

3

You can try to use Edit->Paste Special->Paste JSON as classes command in Visual Studio. You can find more helpful tips and tricks in Visual Studio tips and tricks article

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66