1

挣扎了5个多小时……但可以让它工作。我在一个文本文件中关注了 JSON。

[
{"A":[
    {"Mon2Fri":[
    {"StopNameMain" : "Dr. Manix Road", "StopNameSub" : "GAA HQ", "StartingTime" : "0700,0720,0740,0800,0820,0840,0900,0920,0940,1000,1020,1040,1100,1120,1140,1200,1220,1240,1300,1320,1340,1400,1420,1440,1500,1520,1540,1600,1620,1640,1700,1720,1740,1800,1820,1840,1900,1940,2020,2100,2140,2220,2300,2340"},
    {"StopNameMain" : "Upper Salthill Rd", "StopNameSub" : "Diving B'rd", "StartingTime" : "0703,0723,0743,0803,0823,0843,0903,0923,0943,1003,1023,1043,1103,1123,1143,1203,1223,1243,1303,1323,1343,1403,1423,1443,1503,1523,1543,1603,1623,1643,1703,1723,1743,1803,1823,1843,1903,1943,2023,2103,2143,2223,2303,2343"},
    {"StopNameMain" : "Upper Salthill Road", "StopNameSub" : "Waterfront", "StartingTime" : "0707,0727,0747,0807,0827,0847,0907,0927,0947,1007,1027,1047,1107,1127,1147,1207,1227,1247,1307,1327,1347,1407,1427,1447,1507,1527,1547,1607,1627,1647,1707,1727,1747,1807,1827,1847,1907,1947,2027,2107,2147,2227,2307,2347"},
    {"StopNameMain" : "Salthill Road Lwr", "StopNameSub" : "Lios Ealtan", "StartingTime" : "0711,0731,0751,0811,0831,0851,0911,0931,0951,1011,1031,1051,1111,1131,1151,1211,1231,1251,1311,1331,1351,1411,1431,1451,1511,1531,1551,1611,1631,1651,1711,1731,1751,1811,1831,1851,1911,1951,2031,2111,2151,2231,2311,2351"},
    {"StopNameMain" : "Eyre Square", "StopNameSub" : "Stop 9", "StartingTime" : "0720,0740,0800,0820,0840,0900,0920,0940,1000,1020,1040,1100,1120,1140,1200,1220,1240,1300,1320,1340,1400,1420,1440,1500,1520,1540,1600,1620,1640,1700,1720,1740,1800,1820,1840,1900,1920,2000,2040,2120,2200,2240,2320,0000"}]},
    {"Sat":[
    {"StopNameMain" : "Dr. Manix Road", "StopNameSub" : "GAA HQ", "StartingTime" : "0800,0840,0920,0940,1000,1020,1040,1100,1120,1140,1200,1220,1240,1300,1320,1340,1400,1420,1440,1500,1520,1540,1600,1620,1640,1700,1720,1740,1800,1820,1840,1900,1940,2020,2100,2140,2220,2300,2340"},
    {"StopNameMain" : "Upper Salthill Road", "StopNameSub" : "Diving B'rd", "StartingTime" : "0803,0843,0923,0943,1003,1023,1043,1103,1123,1143,1203,1223,1243,1303,1323,1343,1403,1423,1443,1503,1523,1543,1603,1623,1643,1703,1723,1743,1803,1823,1843,1903,1943,2023,2103,2143,2223,2303,2343"},
    {"StopNameMain" : "Upper Salthill Road", "StopNameSub" : "Waterfront", "StartingTime" : "0807,0847,0927,0947,1007,1027,1047,1107,1127,1147,1207,1227,1247,1307,1327,1347,1407,1427,1447,1507,1527,1547,1607,1627,1647,1707,1727,1747,1807,1827,1847,1907,1947,2027,2107,2147,2227,2307,2347"},
    {"StopNameMain" : "Salthill Road Lwr", "StopNameSub" : "Lios Ealtan", "StartingTime" : "0811,0851,0931,0951,1011,1031,1051,1111,1131,1151,1211,1231,1251,1311,1331,1351,1411,1431,1451,1511,1531,1551,1611,1631,1651,1711,1731,1751,1811,1831,1851,1911,1951,2031,2111,2151,2231,2311,2351"},
    {"StopNameMain" : "Eyre Square", "StopNameSub" : "Stop 9", "StartingTime" : "0820,0900,0940,1000,1020,1040,1100,1120,1140,1200,1220,1240,1300,1320,1340,1400,1420,1440,1500,1520,1540,1600,1620,1640,1700,1720,1740,1800,1820,1840,1900,1920,2000,2040,2120,2200,2240,2320,0000"}]},
    {"Sun":[
    {"StopNameMain" : "Dr. Manix Road", "StopNameSub" : "GAA HQ", "StartingTime" : "1020,1100,1140,1220,1300,1340,1420,1500,1540,1620,1700,1740,1820,1900,1940,2020,2100,2140,2220,2300,2340"},
    {"StopNameMain" : "Upper Salthill Road", "StopNameSub" : "Diving B'rd", "StartingTime" : "1023,1103,1143,1223,1303,1343,1423,1503,1543,1623,1703,1743,1823,1903,1943,2023,2103,2143,2223,2303,2343"},
    {"StopNameMain" : "Upper Salthill Road", "StopNameSub" : "Waterfront", "StartingTime" : "1027,1107,1147,1227,1307,1347,1427,1507,1547,1627,1707,1747,1827,1907,1947,2027,2107,2147,2227,2307,2347"},
    {"StopNameMain" : "Salthill Road Lwr", "StopNameSub" : "Lios Ealtan", "StartingTime" : "1031,1111,1151,1231,1311,1351,1431,1511,1551,1631,1711,1751,1831,1911,1951,2031,2111,2151,2231,2311,2351"},
    {"StopNameMain" : "Eyre Square", "StopNameSub" : "Stop 9", "StartingTime" : "1040,1120,1200,1240,1320,1400,1440,1520,1600,1640,1720,1800,1840,1920,2000,2040,2120,2200,2240,2320,0000"}]}
]},
{"B":[
    {"Mon2Fri":[
    {"StopNameMain" : "Eyre Square", "StopNameSub" : "Stop 9", "StartingTime" :                "0640,0700,0720,0740,0800,0820,0840,0900,0920,0940,1000,1020,1040,1100,1120,1140,1200,1220,1240,1300,1320,1340,1400,1420,1440,1500,1520,1540,1600,1620,1640,1700,1720,1740,1800,1820,1840,1900,1920,2000,2040,2120,2200,2240,2320"},
    {"StopNameMain" : "Fr.Griffin Rd", "StopNameSub" : "Fr.Burke Pk", "StartingTime" :         "0644,0704,0724,0744,0804,0824,0844,0904,0924,0944,1004,1024,1044,1104,1124,1144,1204,1224,1244,1304,1324,1344,1404,1424,1444,1504,1524,1544,1604,1624,1644,1704,1724,1744,1804,1824,1844,1904,1924,2004,2044,2124,2204,2244,2324"},
    {"StopNameMain" : "Salthill Rd Lwr", "StopNameSub" : "Lios Ealtan", "StartingTime" :       "0647,0707,0727,0747,0807,0827,0847,0907,0927,0947,1007,1027,1047,1107,1127,1147,1207,1227,1247,1307,1327,1347,1407,1427,1447,1507,1527,1547,1607,1627,1647,1707,1727,1747,1807,1827,1847,1907,1927,2007,2047,2127,2207,2247,2327"},
    {"StopNameMain" : "Upper Salthill Road", "StopNameSub" : "C'ton Kitchen", "StartingTime" : "0652,0712,0732,0752,0812,0832,0852,0912,0932,0952,1012,1032,1052,1112,1132,1152,1212,1232,1252,1312,1332,1352,1412,1432,1452,1512,1532,1552,1612,1632,1652,1712,1732,1752,1812,1832,1852,1912,1932,2012,2052,2132,2212,2252,2332"},
    {"StopNameMain" : "Dr Manix Road", "StopNameSub" : "GAA HQ", "StartingTime" :              "0700,0720,0740,0800,0820,0840,0900,0920,0940,1000,1020,1040,1100,1120,1140,1200,1220,1240,1300,1320,1340,1400,1420,1440,1500,1520,1540,1600,1620,1640,1700,1720,1740,1800,1820,1840,1900,1920,1940,2020,2100,2140,2220,2300,2340"}]},
    {"Sat":[
    {"StopNameMain" : "Eyre Square", "StopNameSub" : "Stop 9", "StartingTime" :                "0740,0820,0900,0940,1000,1020,1040,1100,1120,1140,1200,1220,1240,1300,1320,1340,1400,1420,1440,1500,1520,1540,1600,1620,1640,1700,1720,1740,1800,1820,1840,1900,1920,2000,2040,2120,2200,2240,2320"},
    {"StopNameMain" : "Fr.Griffin Rd", "StopNameSub" : "Fr.Burke Pk", "StartingTime" :         "0744,0824,0904,0944,1004,1024,1044,1104,1124,1144,1204,1224,1244,1304,1324,1344,1404,1424,1444,1504,1524,1544,1604,1624,1644,1704,1724,1744,1804,1824,1844,1904,1924,2004,2044,2124,2204,2244,2324"},
    {"StopNameMain" : "Salthill Rd Lwr", "StopNameSub" : "Lios Ealtan", "StartingTime" :       "0747,0827,0907,0947,1007,1027,1047,1107,1127,1147,1207,1227,1247,1307,1327,1347,1407,1427,1447,1507,1527,1547,1607,1627,1647,1707,1727,1747,1807,1827,1847,1907,1927,2007,2047,2127,2207,2247,2327"},
    {"StopNameMain" : "Upper Salthill Road", "StopNameSub" : "C'ton Kitchen", "StartingTime" : "0752,0832,0912,0952,1012,1032,1052,1112,1132,1152,1212,1232,1252,1312,1332,1352,1412,1432,1452,1512,1532,1552,1612,1632,1652,1712,1732,1752,1812,1832,1852,1912,1932,2012,2052,2132,2212,2252,2332"},
    {"StopNameMain" : "Dr. Manix Road", "StopNameSub" : "GAA HQ", "StartingTime" :             "0800,0840,0920,1000,1020,1040,1100,1120,1140,1200,1220,1240,1300,1320,1340,1400,1420,1440,1500,1520,1540,1600,1620,1640,1700,1720,1740,1800,1820,1840,1900,1920,1940,2020,2100,2140,2220,2300,2340"}]},
    {"Sun":[
    {"StopNameMain" : "Eyre Square", "StopNameSub" : "Stop 9", "StartingTime" :                "1000,1040,1120,1200,1240,1320,1400,1440,1520,1600,1640,1720,1800,1840,1920,2000,2040,2120,2200,2240,2320"},
    {"StopNameMain" : "Fr.Griffin Rd", "StopNameSub" : "Fr.Burke Pk", "StartingTime" :         "1004,1044,1124,1204,1244,1324,1404,1444,1524,1604,1644,1724,1804,1844,1924,2004,2044,2124,2204,2244,2324"},
    {"StopNameMain" : "Salthill Rd Lwr", "StopNameSub" : "Lios Ealtan", "StartingTime" :       "1007,1047,1127,1207,1247,1327,1407,1447,1527,1607,1647,1727,1807,1847,1927,2007,2047,2127,2207,2247,2327"},
    {"StopNameMain" : "Upper Salthill Road", "StopNameSub" : "C'ton Kitchen", "StartingTime" : "1012,1052,1132,1212,1252,1332,1412,1452,1532,1612,1652,1732,1812,1852,1932,2012,2052,2132,2212,2252,2332"},
    {"StopNameMain" : "Dr. Manix Road", "StopNameSub" : "GAA HQ", "StartingTime" :             "1020,1100,1140,1220,1300,1340,1420,1500,1540,1620,1700,1740,1820,1900,1940,2020,2100,2140,2220,2300,2340"}]}
]}
]

我有这个使用 jsoncharp.net 编写的 C# 类

public class Mon2Fri
{
    public string[] StopNameMain { get; set; }
    public string[] StopNameSub { get; set; }
    public string[] StartingTime { get; set; }
}

public class Sat
{
    public string[] StopNameMain { get; set; }
    public string[] StopNameSub { get; set; }
    public string[] StartingTime { get; set; }
}

public class Sun
{
    public string[] StopNameMain { get; set; }
    public string[] StopNameSub { get; set; }
    public string[] StartingTime { get; set; }
}

public class A
{
    public List<Mon2Fri> Mon2Fri { get; set; }
    public List<Sat> Sat { get; set; }
    public List<Sun> Sun { get; set; }
}

public class Mon2Fri2
{
    public string[] StopNameMain { get; set; }
    public string[] StopNameSub { get; set; }
    public string[] StartingTime { get; set; }
}

public class Sat2
{
    public string[] StopNameMain { get; set; }
    public string[] StopNameSub { get; set; }
    public string[] StartingTime { get; set; }
}

public class Sun2
{
    public string[] StopNameMain { get; set; }
    public string[] StopNameSub { get; set; }
    public string[] StartingTime { get; set; }
}

public class B
{
    public List<Mon2Fri2> Mon2Fri { get; set; }
    public List<Sat2> Sat { get; set; }
    public List<Sun2> Sun { get; set; }
}

public class RootObject
{
    public List<A> A { get; set; }
    public List<B> B { get; set; }
}

当我尝试使用解析 json

List<RootObject> deserializedJSON = await JsonConvert.DeserializeObjectAsync<List<RootObject>>(JsonString);

我得到错误对象

{"Object reference not set to an instance of an object."}

如果我尝试使用

RootObject deserializedJSON = await JsonConvert.DeserializeObjectAsync<RootObject>(JsonString);

我得到一个错误

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'GalwayBuses.RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

Path '', line 1, position 1.

我非常感谢一些帮助。我已经在 stack over flow 上浏览了许多帖子,并使用多个网站检查和验证了 json ......只是无法理解这个......

4

1 回答 1

0

终于得到了这个工作......我的一个巨大的疏忽......当我自己阅读json数据时,我意识到了这些问题

相应的类应该是

public class Mon2Fri
{
    public string StopNameMain { get; set; }
    public string StopNameSub { get; set; }
    public string StartingTime { get; set; }
}

public class Sat
{
    public string StopNameMain { get; set; }
    public string StopNameSub { get; set; }
    public string StartingTime { get; set; }
}

public class Sun
{
    public string StopNameMain { get; set; }
    public string StopNameSub { get; set; }
    public string StartingTime { get; set; }
}

public class A
{
    public Mon2Fri[] Mon2Fri { get; set; }
    public Sat[] Sat { get; set; }
    public Sun[] Sun { get; set; }
}

public class Mon2Fri2
{
    public string StopNameMain { get; set; }
    public string StopNameSub { get; set; }
    public string StartingTime { get; set; }
}

public class Sat2
{
    public string StopNameMain { get; set; }
    public string StopNameSub { get; set; }
    public string StartingTime { get; set; }
}

public class Sun2
{
    public string StopNameMain { get; set; }
    public string StopNameSub { get; set; }
    public string StartingTime { get; set; }
}

public class B
{
    public Mon2Fri2[] Mon2Fri { get; set; }
    public Sat2[] Sat { get; set; }
    public Sun2[] Sun { get; set; }
}

public class RootObject
{
    public A[] A { get; set; }
    public B[] B { get; set; }
}

然后它使用

RootObject[] deserializedJSON = await JsonConvert.DeserializeObjectAsync<RootObject[]>(JsonString);
于 2013-04-07T01:07:47.757 回答