Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
111 views
in Technique[技术] by (71.8m points)

c# - Json.NET - format an array of objects with names

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...