I have the following class with a static method in it,
public static class RouteSerializer
{
public static string SerializeRouteInformation(string content)
{
RouteMapModel routeMapModel = new RouteMapModel();
List<RouteMapModel.end_location> endLocationList = new List<RouteMapModel.end_location>();
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();
var serializedEndLocation = JsonConvert.DeserializeObject<RouteMapModel.end_location>(endLocation);
endLocationList.Add(serializedEndLocation);
}
//... goes on
}
}
But, I got an Error on the begging of the second foreach
as it says Newtonsoft.Json.Linq.JValue cannot be assigned to Newtonsoft.Json.Linq.JObject
. But the problem is, if I changed item type from JObject to JValue on the latter foreach
clauses, I cannot be able to reach .GetValue()
method as JValue doesn't inclued a one.
Below is the JSON string that I'm playing on,
{ "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"
}
I do not prefer to deserialize the whole object as service is not going to use nearly all these properties except "legs"
, "steps"
, and "end_location"
which is inside of "steps"
.