ここにJSONがあります。私はこのJSONから3つの個別の変数に3つのステーションコードを取得しようとしています。逆シリアル化しました。リストを反復して、逆シリアル化されたJSON C#ASP.NETから変数を生成する方法
"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つの別々の局コード変数を生成するために、それらthorugh反復処理するために駅コードのリストを作ることを試みてきました。
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];
}
ご協力いただきありがとうございます。
foreach (JToken result in results)
{
// This gives you the current station object from the JToken
Station stationResult = result.ToObject<Station>();
// Add to your Station list
stationResults.Add(stationResult);
}
これはあなたのstationResults
リストの3 Station
を与える:
あなたの問題は何ですか? – derpirscher
私はJSONから各station_codeに3つの異なる変数を生成しようとしています – kieron