0

I have an array like this:

{
"214460106": {
    "HALTESTELLEN_ID": "214460106",
    "TYP": "stop",
    "DIVA": "60200001",
    "NAME": "Absberggasse",
    "GEMEINDE": "Wien",
    "GEMEINDE_ID": "90000",
    "WGS84_LAT": "48.1738010728644",
    "WGS84_LON": "16.3898072745249",
    "STAND": "",
    "PLATFORMS": [{
        "LINIE": "6",
        "ECHTZEIT": "1",
        "VERKEHRSMITTEL": "ptTram",
        "RBL_NUMMER": "406",
        "BEREICH": "0",
        "RICHTUNG": "H",
        "REIHENFOLGE": "16",
        "STEIG": "6-H",
        "STEIG_WGS84_LAT": "48.173825035357",
        "STEIG_WGS84_LON": "16.3894569315641"
    },
    {
        "LINIE": "6",
        "ECHTZEIT": "1",
        "VERKEHRSMITTEL": "ptTram",
        "RBL_NUMMER": "420",
        "BEREICH": "0",
        "RICHTUNG": "R",
        "REIHENFOLGE": "19",
        "STEIG": "6-R",
        "STEIG_WGS84_LAT": "48.1739867818893",
        "STEIG_WGS84_LON": "16.3898162576777"
    },
    {
        "LINIE": "N6",
        "ECHTZEIT": "1",
        "VERKEHRSMITTEL": "ptBusNight",
        "RBL_NUMMER": "406",
        "BEREICH": "0",
        "RICHTUNG": "H",
        "REIHENFOLGE": "13",
        "STEIG": "N6-H",
        "STEIG_WGS84_LAT": "48.1738010728644",
        "STEIG_WGS84_LON": "16.3892682853544"
    },
    {
        "LINIE": "N6",
        "ECHTZEIT": "1",
        "VERKEHRSMITTEL": "ptBusNight",
        "RBL_NUMMER": "420",
        "BEREICH": "0",
        "RICHTUNG": "R",
        "REIHENFOLGE": "6",
        "STEIG": "N6-R",
        "STEIG_WGS84_LAT": "48.1740406972867",
        "STEIG_WGS84_LON": "16.3896994766908"
    }],
    "LINES": ["6",
    "N6"]
},
"214460107": {
    "HALTESTELLEN_ID": "214460107",
    "TYP": "stop",
    "DIVA": "60200002",
    "NAME": "Achengasse",
    "GEMEINDE": "Wien",
    "GEMEINDE_ID": "90000",
    "WGS84_LAT": "48.2845258075837",
    "WGS84_LON": "16.4488984539143",
    "STAND": "",
    "PLATFORMS": [{
        "LINIE": "32A",
        "ECHTZEIT": "1",
        "VERKEHRSMITTEL": "ptBusCity",
        "RBL_NUMMER": "1168",
        "BEREICH": "0",
        "RICHTUNG": "H",
        "REIHENFOLGE": "7",
        "STEIG": "32A-H",
        "STEIG_WGS84_LAT": "48.284334521556",
        "STEIG_WGS84_LON": "16.4489523528313"
    },
    {
        "LINIE": "32A",
        "ECHTZEIT": "1",
        "VERKEHRSMITTEL": "ptBusCity",
        "RBL_NUMMER": "1159",
        "BEREICH": "0",
        "RICHTUNG": "R",
        "REIHENFOLGE": "35",
        "STEIG": "32A-R",
        "STEIG_WGS84_LAT": "48.2844540754073",
        "STEIG_WGS84_LON": "16.4509825453734"
    }],
    "LINES": ["32A"]
},
... and so on

Is there any way to format something like this in a List<class>? I tried to do it but he always stops at the 21460106 number. Also tried json2csharp but that only makes 5000 classes where every class has the number as its name.

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
user3024750
  • 105
  • 1
  • 11
  • The provided JSON is not an array. It should look like `[{....}, {...}]`, instead every item is a property of a very large object and each number is a property of this object. Perhaps you have the possibilty to change the generated JSON. – Ralf Bönning Aug 27 '16 at 08:55
  • The problem I see is that this is not an array, but a single object with properties `214460106`, `214460107` etc. – Ivan Stoev Aug 27 '16 at 08:57

2 Answers2

2

Your outer JSON container isn't an array, it's a JSON object: a comma-delimited set of name/value pairs surrounded by braces. Since the names are arbitrary keys and not fixed, the easiest way to deserialize this is as a dictionary.

Types:

public class PLATFORM
{
    public string LINIE { get; set; }
    public string ECHTZEIT { get; set; }
    public string VERKEHRSMITTEL { get; set; }
    public string RBL_NUMMER { get; set; }
    public string BEREICH { get; set; }
    public string RICHTUNG { get; set; }
    public string REIHENFOLGE { get; set; }
    public string STEIG { get; set; }
    public string STEIG_WGS84_LAT { get; set; }
    public string STEIG_WGS84_LON { get; set; }
}

public class RootObject
{
    public string HALTESTELLEN_ID { get; set; }
    public string TYP { get; set; }
    public string DIVA { get; set; }
    public string NAME { get; set; }
    public string GEMEINDE { get; set; }
    public string GEMEINDE_ID { get; set; }
    public string WGS84_LAT { get; set; }
    public string WGS84_LON { get; set; }
    public string STAND { get; set; }
    public List<PLATFORM> PLATFORMS { get; set; }
    public List<string> LINES { get; set; }
}

Deserialization code:

var dict = JsonConvert.DeserializeObject<Dictionary<long, RootObject>>(json);

I chose long as the key type since all of your root property names look to be integral strings. You could use Dictionary<string, RootObject> if not.

To generate the RootObject class I copied the JSON from one of the values in the root object -- the value of "214460106" in this case -- to http://json2csharp.com/.

Sample fiddle.

For related documentation see: Deserialize a Dictionary.

dbc
  • 104,963
  • 20
  • 228
  • 340
0

I don't believe it's possible to use a number as a field, try starting with an alpha character, such as A214460106.

This is just a quick way to test that your formatting is correct. You can convert from list to Json string and back to a datatable just to check the string works correctly without needing to use a model.

  private void button1_Click(object sender, EventArgs e)
    {
        List<Employees> employees = new List<Employees>();
        Employees employee = new Employees();
        employee.firstName = "a";
        employees.Add(employee);

        string json = JsonConvert.SerializeObject(employees);
        DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));
    }



}
public class Employees
{
    public string firstName { get; set; }
    public string lastName { get; set; }

}
Mike
  • 51
  • 5