Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 16, 2012 08:11 AM by awebb
Member
28 Points
35 Posts
Mar 15, 2012 07:08 PM|LINK
Hi
I want to know how to read specific data from JSON .
For example:
"results" : [ { "address_components" : [ { "long_name" : "dsdsdsdi", "short_name" : "sdsdi", "types" : [ "locality", "politichjkal" ] }, { "long_name" : "sdsdsdi", "short_name" : "dsd", "types" : [ "administrative_area_level_1", "politisdscal" ] }, { "long_name" : "sdsd", "short_name" : "sdsd", "types" : [ "country", "political" ] } ], "formatted_address" : "eedfdfdfdfdfdfdf", "geometry" : { "bounds" : { "northeast" : { "lat" : 56.88225340, "lng" : 7.34169940 }, "southwest" : { "lat" : 2.4792219750, "lng" : 6.85382840 }
In this example i want to read southwest lat and lng only, so how to do it?
475 Points
99 Posts
Mar 15, 2012 07:30 PM|LINK
You could use Json.Net's Linq To Json.
http://james.newtonking.com/pages/json-net.aspx
Available on NuGet
It appears you have an array of "results" (this appears to be an incomplete Json segment). Assuming you have an object with an array of "results".
You could load the entire Json string with
var jObject= JObject.Parse(string).
Then, select the the results array:
JArray results = jObject["results"]
Now, loop through the array of results object finding your target "result", then the geometry object and finally the southwest object...
183 Points
39 Posts
Microsoft
Mar 15, 2012 07:31 PM|LINK
One way is to use Json.NET's to query LINQ to JSON -- in particular use SelectToken(...)
http://james.newtonking.com/projects/json/help/SelectToken.html
Say this is your data (it is your example simplified):
var s = @"{'results' : [ { 'address_components' : 'abc' , 'formatted_address' : 'eedfdfdfdfdfdfdf', 'geometry' : { 'bounds' : { 'northeast' : { 'lat' : 56.88225340, 'lng' : 7.34169940 }, 'southwest' : { 'lat' : 2.4792219750, 'lng' : 6.85382840 }}}}]}";
Here is how you can get the 'lat' and 'lng' in southwest:
JObject o = JObject.Parse(s); JArray arr = (JArray) o.SelectToken("results"); JObject southwest = (JObject) arr[0].SelectToken("geometry").SelectToken("bounds").SelectToken("southwest"); var southwestLat = southwest.SelectToken("lat"); var southwestLng = southwest.SelectToken("lng"); Console.WriteLine(southwest); Console.WriteLine(southwestLat); Console.WriteLine(southwestLng);
Thanks, Maggie Ying
265 Points
105 Posts
Mar 15, 2012 09:56 PM|LINK
Why are you double posting? I already gave you an answer in your other post :)
http://forums.asp.net/p/1780347/4880230.aspx/1?How+to+read+JSON+
204 Points
91 Posts
Mar 16, 2012 08:11 AM|LINK
maggie.ying One way is to use Json.NET's to query LINQ to JSON
I up-vote that. LINQ to JSON is a beautiful thing. I use custom media type formatters for all my resource representations, written using LINQ to XML and LINQ to JSON... a killer combination.
bihariagrawa...
Member
28 Points
35 Posts
How to traverse in json?
Mar 15, 2012 07:08 PM|LINK
Hi
I want to know how to read specific data from JSON .
For example:
"results" : [ { "address_components" : [ { "long_name" : "dsdsdsdi", "short_name" : "sdsdi", "types" : [ "locality", "politichjkal" ] }, { "long_name" : "sdsdsdi", "short_name" : "dsd", "types" : [ "administrative_area_level_1", "politisdscal" ] }, { "long_name" : "sdsd", "short_name" : "sdsd", "types" : [ "country", "political" ] } ], "formatted_address" : "eedfdfdfdfdfdfdf", "geometry" : { "bounds" : { "northeast" : { "lat" : 56.88225340, "lng" : 7.34169940 }, "southwest" : { "lat" : 2.4792219750, "lng" : 6.85382840 }jdothoffman
Member
475 Points
99 Posts
Re: How to traverse in json?
Mar 15, 2012 07:30 PM|LINK
You could use Json.Net's Linq To Json.
http://james.newtonking.com/pages/json-net.aspx
Available on NuGet
It appears you have an array of "results" (this appears to be an incomplete Json segment). Assuming you have an object with an array of "results".
You could load the entire Json string with
var jObject= JObject.Parse(string).
Then, select the the results array:
JArray results = jObject["results"]
Now, loop through the array of results object finding your target "result", then the geometry object and finally the southwest object...
-Blog
maggie.ying
Member
183 Points
39 Posts
Microsoft
Re: How to traverse in json?
Mar 15, 2012 07:31 PM|LINK
One way is to use Json.NET's to query LINQ to JSON -- in particular use SelectToken(...)
http://james.newtonking.com/projects/json/help/SelectToken.html
Say this is your data (it is your example simplified):
var s = @"{'results' : [ { 'address_components' : 'abc' , 'formatted_address' : 'eedfdfdfdfdfdfdf', 'geometry' : { 'bounds' : { 'northeast' : { 'lat' : 56.88225340, 'lng' : 7.34169940 }, 'southwest' : { 'lat' : 2.4792219750, 'lng' : 6.85382840 }}}}]}";Here is how you can get the 'lat' and 'lng' in southwest:
JObject o = JObject.Parse(s); JArray arr = (JArray) o.SelectToken("results"); JObject southwest = (JObject) arr[0].SelectToken("geometry").SelectToken("bounds").SelectToken("southwest"); var southwestLat = southwest.SelectToken("lat"); var southwestLng = southwest.SelectToken("lng"); Console.WriteLine(southwest); Console.WriteLine(southwestLat); Console.WriteLine(southwestLng);Thanks,
Maggie Ying
SiggiGG
Member
265 Points
105 Posts
Re: How to traverse in json?
Mar 15, 2012 09:56 PM|LINK
Why are you double posting? I already gave you an answer in your other post :)
http://forums.asp.net/p/1780347/4880230.aspx/1?How+to+read+JSON+
awebb
Member
204 Points
91 Posts
Re: How to traverse in json?
Mar 16, 2012 08:11 AM|LINK
I up-vote that. LINQ to JSON is a beautiful thing. I use custom media type formatters for all my resource representations, written using LINQ to XML and LINQ to JSON... a killer combination.