CSharp · Console
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using NUnit.Framework; // Throws NUnit.Framework.AssertionException
//https://stackoverflow.com/questions/54770959/cannot-convert-from-jvalue-to-jobject
public class Location
{
public double Lat { get; set; }
public double Lng { get; set; }
}
class TestClass
{
public static void Test()
{
TestFix();
try
{
TestOld();
}
catch (Exception ex)
{
Console.WriteLine("Exception from old code: ");
Console.WriteLine(ex);
}
}
public static void TestFix()
{
var content = GetJson();
var obj = JObject.Parse(content);
var endLocationList = obj
.SelectTokens("routes[*].legs[*].steps[*].end_location")
.Select(t => t.ToObject<Location>())
.ToList();
Console.WriteLine("End locations using fixed code: ");
Console.WriteLine(JsonConvert.SerializeObject(endLocationList, Formatting.Indented));
}
public static void TestOld()
{
var content = GetJson();
var obj = JObject.Parse(content);
string objRoutes = obj["routes"].ToString();
JArray routeArray = JArray.Parse(objRoutes);
JArray legArray = new JArray();
foreach (JObject item in routeArray)
{
string leg = item.GetValue("legs").ToString();
legArray.Add(leg);
}
JArray stepArray = new JArray();
foreach (JObject item in legArray)
{
string step = item.GetValue("steps").ToString();
stepArray.Add(step);
}
foreach (JObject item in stepArray)
{
string endLocation = item.GetValue("end_location").ToString();
Console.WriteLine(endLocation);
//var serializedEndLocation = JsonConvert.DeserializeObject<RouteMapModel.end_location>(endLocation);
//endLocationList.Add(serializedEndLocation);
}
}
static string GetJson()
{
var json = @" { ""geocoded_waypoints"" : [
{
""geocoder_status"" : ""OK"",
""place_id"" : ""ChIJ5cGzCGa4yhQRk-lsJUoyizk"",
""types"" : [ ""street_address"" ]
},
{
""geocoder_status"" : ""OK"",
""place_id"" : ""ChIJb5VnEF64yhQRCrgps2g77jc"",
""types"" : [ ""street_address"" ]
}
],
""routes"" : [
{
""bounds"" : {
""northeast"" : {
""lat"" : 40.990381,
""lng"" : 29.0282547
},
""southwest"" : {
""lat"" : 40.9863897,
""lng"" : 29.0216118
}
},
""copyrights"" : ""Harita verileri ©2019 Google"",
""legs"" : [
{
""distance"" : {
""text"" : ""0,9 km"",
""value"" : 894
},
""duration"" : {
""text"" : ""11 dakika"",
""value"" : 655
},
""end_address"" : ""Caferağa Mahallesi, Tuğlacı Eminbey Cd. No:5, 34710 Kadıköy/İstanbul, Türkiye"",
""end_location"" : {
""lat"" : 40.9863897,
""lng"" : 29.0218105
},
""start_address"" : ""Osmanağa Mahallesi, Serasker Cd. No:118, 34714 Kadıköy/İstanbul, Türkiye"",
""start_location"" : {
""lat"" : 40.9893375,
""lng"" : 29.028225
},
""steps"" : [
{
""distance"" : {
""text"" : ""8 m"",
""value"" : 8
},
""duration"" : {
""text"" : ""1 dakika"",
""value"" : 5
},
""end_location"" : {
""lat"" : 40.9894537,
""lng"" : 29.0282117
},
""html_instructions"" : ""\u003cb\u003eZiya Bey Sk.\u003c/b\u003e adlı yerden \u003cb\u003eSerasker Cd.\u003c/b\u003e hedefine \u003cb\u003ekuzey\u003c/b\u003e yönünde ilerleyin"",
""polyline"" : {
""points"" : ""kvdyFkqdpDGCMA?F""
},
""start_location"" : {
""lat"" : 40.9893375,
""lng"" : 29.028225
},
""travel_mode"" : ""WALKING""
},
{
""distance"" : {
""text"" : ""0,3 km"",
""value"" : 326
},
""duration"" : {
""text"" : ""4 dakika"",
""value"" : 215
},
""end_location"" : {
""lat"" : 40.990381,
""lng"" : 29.024582
},
""html_instructions"" : ""\u003cb\u003eSerasker Cd.\u003c/b\u003e yönünde \u003cb\u003esola\u003c/b\u003e dönün"",
""maneuver"" : ""turn-left"",
""polyline"" : {
""points"" : ""awdyFiqdpDMxAKr@OjACP[vCEPC^Cf@CLAHENMZ@LQf@Qd@KZENSj@""
},
""start_location"" : {
""lat"" : 40.9894537,
""lng"" : 29.0282117
},
""travel_mode"" : ""WALKING""
},
{
""distance"" : {
""text"" : ""34 m"",
""value"" : 34
},
""duration"" : {
""text"" : ""1 dakika"",
""value"" : 25
},
""end_location"" : {
""lat"" : 40.9901185,
""lng"" : 29.0243691
},
""html_instructions"" : ""\u003cb\u003eMühürdar Cd.\u003c/b\u003e yönünde \u003cb\u003esola\u003c/b\u003e dönün"",
""maneuver"" : ""turn-left"",
""polyline"" : {
""points"" : ""{|dyFszcpDHFHF^X""
},
""start_location"" : {
""lat"" : 40.990381,
""lng"" : 29.024582
},
""travel_mode"" : ""WALKING""
},
{
""distance"" : {
""text"" : ""8 m"",
""value"" : 8
},
""duration"" : {
""text"" : ""1 dakika"",
""value"" : 5
},
""end_location"" : {
""lat"" : 40.990081,
""lng"" : 29.0242851
},
""html_instructions"" : ""\u003cb\u003eMühürdar Cd.\u003c/b\u003e yönünde hafif \u003cb\u003esağa\u003c/b\u003e yönelin"",
""maneuver"" : ""turn-slight-right"",
""polyline"" : {
""points"" : ""g{dyFiycpD?D?@?@@??@DB""
},
""start_location"" : {
""lat"" : 40.9901185,
""lng"" : 29.0243691
},
""travel_mode"" : ""WALKING""
},
{
""distance"" : {
""text"" : ""7 m"",
""value"" : 7
},
""duration"" : {
""text"" : ""1 dakika"",
""value"" : 6
},
""end_location"" : {
""lat"" : 40.9900274,
""lng"" : 29.0243112
},
""html_instructions"" : ""\u003cb\u003eMuvakkıthane Cd.\u003c/b\u003e konumunda \u003cb\u003esola\u003c/b\u003e dönün"",
""maneuver"" : ""turn-left"",
""polyline"" : {
""points"" : ""_{dyFyxcpDDABA""
},
""start_location"" : {
""lat"" : 40.990081,
""lng"" : 29.0242851
},
""travel_mode"" : ""WALKING""
},
{
""distance"" : {
""text"" : ""0,4 km"",
""value"" : 352
},
""duration"" : {
""text"" : ""4 dakika"",
""value"" : 259
},
""end_location"" : {
""lat"" : 40.987586,
""lng"" : 29.0217648
},
""html_instructions"" : ""\u003cb\u003eMühürdar Cd.\u003c/b\u003e yönünde \u003cb\u003esağa\u003c/b\u003e dönün"",
""maneuver"" : ""turn-right"",
""polyline"" : {
""points"" : ""uzdyF}xcpDhAz@pA|@pBtAHRJHRPPNPX\\d@BDHKfAlBDLDHDN@LBN""
},
""start_location"" : {
""lat"" : 40.9900274,
""lng"" : 29.0243112
},
""travel_mode"" : ""WALKING""
},
{
""distance"" : {
""text"" : ""63 m"",
""value"" : 63
},
""duration"" : {
""text"" : ""1 dakika"",
""value"" : 51
},
""end_location"" : {
""lat"" : 40.987087,
""lng"" : 29.0216118
},
""html_instructions"" : ""\u003cb\u003eMühürdar Cd.\u003c/b\u003e boyunca ilerlemek için \u003cb\u003esola\u003c/b\u003e dönün"",
""maneuver"" : ""turn-left"",
""polyline"" : {
""points"" : ""mkdyF_icpDNBJ@J@TBJ?JADA@DDN""
},
""start_location"" : {
""lat"" : 40.987586,
""lng"" : 29.0217648
},
""travel_mode"" : ""WALKING""
},
{
""distance"" : {
""text"" : ""74 m"",
""value"" : 74
},
""duration"" : {
""text"" : ""1 dakika"",
""value"" : 64
},
""end_location"" : {
""lat"" : 40.9864708,
""lng"" : 29.0216118
},
""html_instructions"" : ""\u003cb\u003eMühürdar Cd.\u003c/b\u003e boyunca ilerlemek için \u003cb\u003esola\u003c/b\u003e dönün"",
""maneuver"" : ""turn-left"",
""polyline"" : {
""points"" : ""ihdyFahcpDJEJAFALAH?VBj@F""
},
""start_location"" : {
""lat"" : 40.987087,
""lng"" : 29.0216118
},
""travel_mode"" : ""WALKING""
},
{
""distance"" : {
""text"" : ""22 m"",
""value"" : 22
},
""duration"" : {
""text"" : ""1 dakika"",
""value"" : 25
},
""end_location"" : {
""lat"" : 40.9863897,
""lng"" : 29.0218105
},
""html_instructions"" : ""\u003cb\u003eTuğlacı Eminbey Cd.\u003c/b\u003e yönünde \u003cb\u003esola\u003c/b\u003e dönün"",
""maneuver"" : ""turn-left"",
""polyline"" : {
""points"" : ""mddyFahcpDNg@""
},
""start_location"" : {
""lat"" : 40.9864708,
""lng"" : 29.0216118
},
""travel_mode"" : ""WALKING""
}
],
""traffic_speed_entry"" : [],
""via_waypoint"" : []
}
],
""overview_polyline"" : {
""points"" : ""kvdyFkqdpDUEM`B{@hHQfBGXMZ@Lc@lAQj@Sj@HFHF^X?D?B@@DBDABAhAz@bErCHRJHd@`@n@~@BDHKlAzBJXD\\ZD`@DVADA@DDNJERCVAbAJNg@""
},
""summary"" : ""Serasker Cd. ve Mühürdar Cd."",
""warnings"" : [
""Yürüyerek gitmek için yol tarifi beta özelliğinde mevcuttur. Dikkat – Bu rotada kaldırım veya yaya yolu olmayabilir.""
],
""waypoint_order"" : []
}
],
""status"" : ""OK""
}";
return json;
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine();
try
{
TestClass.Test();
}
catch (Exception ex)
{
Console.WriteLine("Failed with unhandled exception: ");
Console.WriteLine(ex);
throw;
}
}
}