【发布时间】:2016-04-07 22:45:55
【问题描述】:
这是我的 JSON。我试图从我反序列化的这个 JSON 中将 3 个站代码转换为 3 个单独的变量。
"stations": [
{
"station_code": "HWV",
"atcocode": null,
"tiploc_code": "HTRWTM5",
"name": "Heathrow Airport Terminal 5",
"mode": "train",
"longitude": -0.490589,
"latitude": 51.470051,
"distance": 369
},
{
"station_code": "HXX",
"atcocode": null,
"tiploc_code": "HTRWAPT",
"name": "Heathrow Airport Central Terminal Area (T123)",
"mode": "train",
"longitude": -0.454333,
"latitude": 51.471404,
"distance": 2309
},
{
"station_code": "HAF",
"atcocode": null,
"tiploc_code": "HTRWTM4",
"name": "Heathrow Airport Terminal 4",
"mode": "train",
"longitude": -0.445463,
"latitude": 51.458266,
"distance": 3336
}
这是我的 C#,我首先为站代码创建了一个类,然后反序列化 JSON,并尝试制作一个站代码列表,然后遍历它们以生成 3 个单独的站代码变量。
public class Station
{
public string station_code { get; set; }
}
JObject json = JObject.Parse(localJson);
IList<JToken> results = json["stations"].Children().ToList();
IList<Station> stationResults = new List<Station>();
foreach (JToken result in results)
{
Station stationResult = JsonConvert.DeserializeObject<Station>(result.ToString());
stationResults.Add(stationResult);
var station1 = stationResults[0];
var station2 = stationResults[0];
var station3 = stationResults[0];
}
任何帮助将不胜感激,谢谢!
【问题讨论】:
-
你有什么问题?
-
我试图从 JSON 中为每个 station_code 生成 3 个不同的变量