2016-04-07 14 views
0

değişkenleri üretmek için bir listede nasıl yinelenir İşte benim JSON'um. 3 istasyon kodunu, bu JSON i serileştirilmiş 3 bireysel değişkene almaya çalışıyorum.Seri halindeki 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 
} 

Ve burada öncelikle istasyon kodu için bir sınıf yaptık benim C#, sonra JSON serileştirilemeyen ve 3 ayrı istasyon kod değişkenleri üretmek için sonra onlara thorugh yineleme için istasyon kodlarının bir listesini yapmak çalıştılar .

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

Herhangi bir yardım için teşekkür ederiz, teşekkür ederim!

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

Bu stationResults Listesi'nde size 3 Station verecektir:

+1

Sorununuz nedir? – derpirscher

+0

JSON – kieron

cevap

0
senin foreach değiştirin

. Listenizdeki her bir istasyona liste indeksini kullanarak erişebilirsiniz (örn. stationResults[0]) veya listenizde yineleyin.

Listenizdeki her öğeyi neden tek bir değişkene atamak istediğinizi anlamıyorum. Ama gerçekten böyle bir şey yapabileceğini size isterseniz: bir dizi türüdür çünkü IList<Station> doğrudan localJson serisini

Station station1 = stationResults[0]; 
Station station2 = stationResults[1]; 
Station station3 = stationResults[2]; 
+0

'dan her bir station_code için 3 farklı değişken üretmeye çalışıyorum 3 serpate tren istasyonundan istasyon kodunu başka bir api bağlantısına sokacağım ve daha sonra her tren istasyonu için zaman çizelgelerini gösterecek – kieron

+0

istasyon_kodunu kullanarak 'stationResults [0] .station_code' vb. İstatistiklerinizin sonuç listenizde bireysel değişkenlere atanmasına gerek yoktur. – Kurorion

0

. Her birini bir foreach döngüsünde serileştirmeye gerek yok.

var stations = JsonConvert.DeserializeObject<IList<Station>>(localJson); 
var station1 = stations[0].station_code; 
var station2 = stations[1].station_code; 
var station3 = stations[2].station_code;