我使用 Bing Map REST API,因为我想从特定位置找出地理位置。Url 效果很好,但由于事实上,我第一次使用 JSON 代码,我真的不知道如何获得我想要的输出。
我尝试了以下方法:
string URL = "http://dev.virtualearth.net/REST/v1/Locations?countryRegion=&adminDistrict=&locality=Wien&postalCode=&addressLine=&userLocation=&userIp=&usermapView=&includeNeighborhood=&maxResults=&key=MY_KEY";
WebClient client = new WebClient();
client.OpenReadAsync(new Uri(URL, UriKind.Absolute));
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
private void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Point));
Point data = serializer.ReadObject(e.Result) as Point;
}
[DataContract]
public class Point
{
/// <summary>
/// Latitude,Longitude
/// </summary>
[DataMember(Name = "coordinates")]
public double[] Coordinates { get; set; }
}
当我在浏览器中输入链接时,这是我的回应:
{"authenticationResultCode":"ValidCredentials","brandLogoUri":"http://dev.virtualearth.net/Branding/logo_powered_by.png","copyright":"版权所有 © 2013 Microsoft 及其供应商。保留所有权利。此未经微软公司明确书面许可,不得访问 API,不得以任何方式使用、复制或传输内容和任何结果。","resourceSets":[{"estimatedTotal":1,"resources":[{" __type":"位置:http://schemas.microsoft.com/search/local/ws/rest/v1","bbox":[47.253395080566406,-123.16571807861328,47.946159362792969,-121.50344085693359],"名称":"西雅图, WA","point":{"type":"Point","coordinates":[47.603561401367188,-122.32943725585938]},"address":{"adminDistrict":"WA","adminDistrict2":"King Co.","countryRegion":"United States","formattedAddress":"Seattle, WA","locality": "Seattle"},"confidence":"High","entityType":"PopulatedPlace","geocodePoints":[{"type":"Point","coordinates":[47.603561401367188,-122.32943725585938],"calculationMethod": "屋顶","usageTypes":["Display"]}],"matchCodes":["Good"]}]}],"statusCode":200,"statusDescription":"OK","traceId":"08bee37ecb714d7cb7e2783eb8f873a4 |LTSM000177|02.00.183.2300|LTSIPEVM000039"}adminDistrict":"WA","adminDistrict2":"King Co.","countryRegion":"United States","formattedAddress":"Seattle, WA","locality":"Seattle"},"confidence":" High","entityType":"PopulatedPlace","geocodePoints":[{"type":"Point","coordinates":[47.603561401367188,-122.32943725585938],"calculationMethod":"Rooftop","usageTypes":["显示"]}],"matchCodes":["Good"]}]}],"statusCode":200,"statusDescription":"OK","traceId":"08bee37ecb714d7cb7e2783eb8f873a4|LTSM000177|02.00.183.2300|LTSIPEVM000039"}adminDistrict":"WA","adminDistrict2":"King Co.","countryRegion":"United States","formattedAddress":"Seattle, WA","locality":"Seattle"},"confidence":" High","entityType":"PopulatedPlace","geocodePoints":[{"type":"Point","coordinates":[47.603561401367188,-122.32943725585938],"calculationMethod":"Rooftop","usageTypes":["显示"]}],"matchCodes":["Good"]}]}],"statusCode":200,"statusDescription":"OK","traceId":"08bee37ecb714d7cb7e2783eb8f873a4|LTSM000177|02.00.183.2300|LTSIPEVM000039"}countryRegion":"United States","formattedAddress":"Seattle, WA","locality":"Seattle"},"confidence":"High","entityType":"PopulatedPlace","geocodePoints":[{" type":"Point","coordinates":[47.603561401367188,-122.32943725585938],"calculationMethod":"Rooftop","usageTypes":["Display"]}],"matchCodes":["Good"]}]} ],"statusCode":200,"statusDescription":"OK","traceId":"08bee37ecb714d7cb7e2783eb8f873a4|LTSM000177|02.00.183.2300|LTSIPEVM000039"}countryRegion":"United States","formattedAddress":"Seattle, WA","locality":"Seattle"},"confidence":"High","entityType":"PopulatedPlace","geocodePoints":[{" type":"Point","coordinates":[47.603561401367188,-122.32943725585938],"calculationMethod":"Rooftop","usageTypes":["Display"]}],"matchCodes":["Good"]}]} ],"statusCode":200,"statusDescription":"OK","traceId":"08bee37ecb714d7cb7e2783eb8f873a4|LTSM000177|02.00.183.2300|LTSIPEVM000039"}entityType":"PopulatedPlace","geocodePoints":[{"type":"Point","coordinates":[47.603561401367188,-122.32943725585938],"calculationMethod":"Rooftop","usageTypes":["Display"]} ],"matchCodes":["Good"]}]}],"statusCode":200,"statusDescription":"OK","traceId":"08bee37ecb714d7cb7e2783eb8f873a4|LTSM000177|02.00.183.2300|LTSIPEVM000039"}entityType":"PopulatedPlace","geocodePoints":[{"type":"Point","coordinates":[47.603561401367188,-122.32943725585938],"calculationMethod":"Rooftop","usageTypes":["Display"]} ],"matchCodes":["Good"]}]}],"statusCode":200,"statusDescription":"OK","traceId":"08bee37ecb714d7cb7e2783eb8f873a4|LTSM000177|02.00.183.2300|LTSIPEVM000039"}statusDescription":"OK","traceId":"08bee37ecb714d7cb7e2783eb8f873a4|LTSM000177|02.00.183.2300|LTSIPEVM000039"}statusDescription":"OK","traceId":"08bee37ecb714d7cb7e2783eb8f873a4|LTSM000177|02.00.183.2300|LTSIPEVM000039"}
我现在的问题是:这是正确的方法吗?我如何获取数据?
我希望你能帮助我,我已经花了很多时间来解决这个问题。
- - -编辑 - - -
现在我使用 JSON.net,但我不知道如何将从网站获得的数据解析为字符串变量。
我尝试了以下方法:
var json = new WebClient().DownloadString("url");
但这不起作用,因为我正在 Windows Phone 上开发。
System.Net.WebClient 不包含“DownloadString”的定义......
任何人都可以帮助我使其适用于 Windows Phone 应用程序。