我試圖創建一個簡單的應用程序,它從Google Maps API獲取JSON文檔並遍歷它,顯示路線,持續時間和距離的每條路線。通過JSON文檔C迭代#
我已經得到它來顯示腿的第一個,但我不知道如何使框重複。也許是一個foreach循環?我是新的,我真的不確定。
var from = origin.Text;
var to = destination.Text;
var requesturl = @"http://maps.googleapis.com/maps/api/directions/json?origin="
+ from + "&alternatives=false&units=imperial&destination="
+ to + "&sensor=false";
string content = file_get_contents(requesturl);
JObject o = JObject.Parse(content);
string distance = (string)o.SelectToken("routes[0].legs[0].distance.text");
string instructions =
(string) o.SelectToken("routes[0].legs[0].steps[0].html_instructions");
txtDistance.Text = distance;
TextBox1.Text = instructions;
您可以(也可能應該)創建一個匹配JSON結構的對象,並將結果反序列化爲它。那麼它將清楚如何迭代它。你可以發佈你回來的json嗎? –
可能的重複[如何通過JObject枚舉?](http://stackoverflow.com/questions/10543512/how-do-i-enumerate-through-a-jobject) – James