c# - foreach loop to fetch JSON value -
i trying fetch latitude , longitude of json result.
json result (have many records):
"results" : [ { "geometry" : { "location" : { "lat" : 28.637255, "lng" : 77.05202800000001 } }, "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png", "id" : "aef56d078ec3fcab6f4a966bd944d3d59973bd72", "name" : "homeopati kendra", "opening_hours" : { "open_now" : false, "weekday_text" : [] }, "place_id" : "chijqavdfrwfdtkrmrahjcbioni", "reference" : "cnrjaaaawcobegdvsl4keqnpyt2osvf82b7fchipuhrfqvgg8b1er7fcv9i1nupn0lff50ovg9ug1pevkczg813lq9aae1dk5gcgn99ajpq1it9lafcwx3sauwtiinliepgptdhnz3ngdzhpvix70a2d1kzcchiqvd4os73_jmr2wyqg4jtrjxoujcngt2m4xzdixadjotga-lgrnr4", "scope" : "google", "types" : [ "hospital", "health", "establishment" ], "vicinity" : "c-29 , vikas nagar,uttam nagar, vikas nagar extn, hastsal, new delhi" },
for particular record (say record first):
jobject obj = jobject.parse(googleresult); jarray jarr = (jarray)obj["results"]; double lt = (double)jarr[0]["geometry"]["location"]["lat"]; double lg = (double)jarr[0]["geometry"]["location"]["lng"];
for fetching records:
foreach(var item in jarr) { double lt = convert.todouble(item[jarr["geometry"]["location"]["lat"]]); }
for fetching 1 record works fine records not working.
you can use selecttokens
pick out fields of interest json. since method supports jsonpath query syntax can use "*"
wildcard loop through entries in "results"
array:
var locations = jtoken.parse(googleresult) .selecttokens("results[*].geometry.location") .select(t => new { lat = (double)t["lat"], lng = (double)t["lng"] }) .tolist();
or, if prefer foreach
loop:
jobject obj = jobject.parse(googleresult); jarray jarr = (jarray)obj["results"]; foreach (var item in jarr) { double lt = (double)item.selecttoken("geometry.location.lat"); double lg = (double)item.selecttoken("geometry.location.lng"); }
Comments
Post a Comment