Json.NET - 格式化带有名称的对象数组

时间:2016-08-27 08:50:20

标签: c# .net json web json.net

我有一个这样的数组:

{
"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<class>中格式化这样的内容?我试图这样做,但他总是停在21460106号码。还尝试了json2csharp,但只有5000个类,每个类都有这个数字作为名称。

2 个答案:

答案 0 :(得分:2)

您的外部JSON容器不是数组,它是JSON object:由大括号包围的逗号分隔的名称/值对集合。由于名称是任意键而不是固定的,因此将其反序列化的最简单方法是作为字典。

类型:

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<string, RootObject>

为了生成RootObject类,我将JSON从根对象中的值的一个复制 - 在这种情况下为"214460106"的值 - 到{{ 3}}

示例http://json2csharp.com/

有关相关文档,请参阅:fiddle

答案 1 :(得分:0)

我不相信可以使用数字作为字段,尝试以字母字符开头,例如A214460106。

这只是测试格式正确的快速方法。 您可以从列表转换为Json字符串并返回到数据表,只是为了检查字符串是否正常工作而无需使用模型。

  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; }

}