を使用してJSONオブジェクトを解析しようとしJavaScriptSerializer().Deserialize
ましたが、foreach (var item in getRoute.results)
どこでnullエラーがスローされましたRoute getRoute = new JavaScriptSerializer().Deserialize<Route>(strresulttest);
strresulttest
内容を印刷してデバッグしたところ、https://codeshare.io/5vM6X4に示すように期待されるJSON出力を確認できました。
JSONを解析するコード:
public class Route
{
public List<GetRoute> results { get; set; }
}
public class GetRoute
{
public string status_message { get; set; }
public string viaRoute { get; set; }
public string subtitle { get; set; }
public int totalTime { get; set; }
public int totalDistance { get; set; }
}
private void GetInstructions()
{
//GET METHOD for route query
string strurltest = String.Format("https://developers.onemap.sg/privateapi/routingsvc/route?start="+
startLat+","+ startLon +"&end="+ destinationLat +","+ destinationLon+"&"+
"routeType="+ transportType + "&token="+token);
WebRequest requestObjGet = WebRequest.Create(strurltest);
requestObjGet.Method = "GET";
HttpWebResponse responseObjGet = null;
responseObjGet = (HttpWebResponse)requestObjGet.GetResponse();
string strresulttest = null;
using (Stream stream = responseObjGet.GetResponseStream())
{
StreamReader sr = new StreamReader(stream);
strresulttest = sr.ReadToEnd();
//reminder: remove after prod. GET is working.
System.Diagnostics.Debug.WriteLine(strresulttest);
sr.Close();
}
//display search recommendations
Route getRoute = new JavaScriptSerializer().Deserialize<Route>(strresulttest);
foreach (var item in getRoute.results)
{
//reminder: remove after prod.
System.Diagnostics.Debug.WriteLine("Route via: " + item.viaRoute + "\n");
System.Diagnostics.Debug.WriteLine("Description: " + item.subtitle + "\n");
}
}
期待されるJSON出力(の場合に表示System.Diagnostics.Debug.WriteLine(strresulttest);
):
{
"status_message": "Found route between points",
"route_geometry": "m{`G_cyxRaAmALMtAyAj@g@RIZG|@KTC??RCxBQ\\KAOa@sDAQWwBM_AUmBEa@Ky@_@kDKoAIgAAs@Ce@?M|@_@PINUFk@Ik@e@aCu@wBeBoD_A}AmBqC{@iA_AeAyAqA{LmKmAu@k@g@y@Jk@r@k@r@GP@PFJhAt@oEnGw@i@QTMPs@g@}AzBr@d@JQ",
"status": 0,
"route_instructions": [
[
"Head",
"SINARAN DRIVE",
56,
"1.320394,103.844478",
18,
"56m",
"North East",
"North",
"driving",
"Head Northeast On Sinaran Drive"
],
[
...
],
...
],
"route_name": [
"MOULMEIN ROAD",
"WHAMPOA ROAD"
],
"route_summary": {
"start_point": "SINARAN DRIVE",
"end_point": "WHAMPOA ROAD",
"total_time": 390,
"total_distance": 2675
},
"viaRoute": "JALAN BAHAGIA",
"subtitle": "Fastest route, assuming usual traffic",
"phyroute": {
"status_message": "Found route between points",
"route_geometry": "m{`G_cyxRaAmALMtAyAj@g@RIZG|@KTC??Ao@BcB?kCBIHK|Ay@M_AUmBEa@Ky@_@kDKoAIgAAs@Ce@?M?OBw@BsCEq@Q{@Qm@KQGMaCNM@iFXO?sCNo@LCa@QaICe@EI?EBQTi@Ha@@c@CMGWOYOOgEgD{BeBuB}AoEnGw@i@QTMPs@g@}AzBr@d@JQ",
"status": 0,
"route_instructions": [
[
"Head",
"SINARAN DRIVE",
56,
"1.320394,103.844478",
18,
"56m",
"North East",
"North",
"driving",
"Head Northeast On Sinaran Drive"
],
[
...
],
...
],
"route_name": [
...
],
"route_summary": {
...
},
"viaRoute": "BALESTIER ROAD",
"subtitle": "Shortest distance"
}
}
results
デシリアライズするクラス構造がJSONと一致しないため、nullが発生します。質問でリンクしたJSONは、代わりにこのクラス構造に対応しています。
public class Route
{
public string status_message { get; set; }
public string route_geometry { get; set; }
public int status { get; set; }
public List<List<object>> route_instructions { get; set; }
public List<string> route_name { get; set; }
public RouteSummary route_summary { get; set; }
public string viaRoute { get; set; }
public string subtitle { get; set; }
public Phyroute phyroute { get; set; }
}
public class RouteSummary
{
public string start_point { get; set; }
public string end_point { get; set; }
public int total_time { get; set; }
public int total_distance { get; set; }
}
public class Phyroute
{
public string status_message { get; set; }
public string route_geometry { get; set; }
public int status { get; set; }
public List<List<object>> route_instructions { get; set; }
public List<string> route_name { get; set; }
public RouteSummary route_summary { get; set; }
public string viaRoute { get; set; }
public string subtitle { get; set; }
}
デシリアライズしてviaRoute
、次のsubtitle
ように取得できます。
Route route = new JavaScriptSerializer().Deserialize<Route>(strresulttest);
System.Diagnostics.Debug.WriteLine("Route via: " + route.viaRoute);
System.Diagnostics.Debug.WriteLine("Description: " + route.subtitle);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加