Click here to Skip to main content
15,883,829 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
Hi Friends,

I'm developing a application in that the requirements are like making the country drop down list selected based on the user's country.

For ex: If the user of my application is from Singapore then singapore should be selected in the drop down and if from India , then India should be selected.

To do this I'm using Google API, Google API[^], and it is returning a JSON object. Now I want to fetch only country name from this JSON object.

I kept break point and able to see the returned JSON object in browser. But how to get the Country name from this?

Any suggestions friends...?
Posted
Comments
Tejas Vaishnav 5-Oct-13 1:23am    
you can deserialize your Json and using that demoralized list of you entity you can fetch using linq over it.

Hi Tejas,

Here you go:

Add reference to Newtonsoft.Json;
using Newtonsoft.Json;


Here the code to get Country:

C#
WebClient wc = new WebClient(); // here I'm using Web Client to get the JSON data frm GMaps API for demo., you're free to use of your choice.

var jsonStr = wc.DownloadString("http://maps.googleapis.com/maps/api/geocode/json?latlng=17.37528,78.47444&sensor=false");
           JObject a = JObject.Parse(jsonStr);
           var addresses = a["results"][0]["formatted_address"].ToString();
           var country = addresses.Split(',')[(addresses.Split(',').Length - 1)].Trim();



Hope it helps.

Happy Coding :)
 
Share this answer
 
See this link. You will get an idea regarding how to get data out of it.

Deserialize Json[^]

Regards..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900