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



我有一个这样的数组:

{
"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这个号码上。还尝试了json2sharp,但它只创建了5000个类,每个类都有数字作为其名称。

你的外部JSON容器不是一个数组,它是一个JSON对象:一组逗号分隔的名称/值对,用大括号括起来。由于名称是任意键而不是固定的,因此反序列化它的最简单方法是使用字典。

类型:

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"的值)复制到http://json2csharp.com/。

样本小提琴。

相关文档请参见:反序列化字典

我不相信可以使用数字作为字段,请尝试从alpha字符开始,例如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; }
}

相关内容

  • 没有找到相关文章

最新更新