从GOOGLE API返回的JSON中获取数据
嗨朋友们,
我正在开发一个应用程序,其要求就像根据用户的国家/地区选择国家/地区下拉列表一样。
例如:如果我的申请用户来自新加坡,那么应该在下拉菜单中选择新加坡,如果来自印度,则应选择印度。
要做到这一点,我使用的是Google API, Google API [ ^ ],它返回一个JSON对象。现在我想从这个JSON对象中仅获取国家名称。
我保持断点并且能够在浏览器中看到返回的JSON对象。但是如何从这里获得国家名称?
任何建议朋友......?
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...?
您好Tejas,
您现在的位置:
添加对Newtonsoft.Json的引用;
Hi Tejas,
Here you go:
Add reference to Newtonsoft.Json;
using Newtonsoft.Json;
这里获取国家代码:
Here the code to get Country:
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 :)
查看此链接。您将了解如何从中获取数据。
Deserialize Json [ ^ ]
问候..
See this link. You will get an idea regarding how to get data out of it.
Deserialize Json[^]
Regards..