问题描述
我有一个这样的数组:
{
"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
有没有办法在 List
中格式化这样的东西?我试图这样做,但他总是停在 21460106 号码上.还尝试了 json2csharp,但它只生成了 5000 个类,其中每个类都以数字作为其名称.
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.
推荐答案
你的外部 JSON 容器不是数组,而是一个 JSON 对象:一组用大括号括起来的逗号分隔的名称/值对.由于名称是任意键且不固定,反序列化的最简单方法是作为字典.
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.
类型:
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; }
}
反序列化代码:
var dict = JsonConvert.DeserializeObject<Dictionary<long, RootObject>>(json);
我选择 long
作为键类型,因为您所有的根属性名称看起来都是整数字符串.如果没有,您可以使用 Dictionary
.
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.
为了生成 RootObject
类,我从根对象中的 one 值中复制了 JSON——"214460106"
的值在这种情况下 -- http://json2csharp.com/.
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/.
示例 fiddle.
有关文档,请参阅:反序列化字典.
这篇关于Json.NET - 用名称格式化对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!