从C#中的Bing Map API解析JSON代码
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了从C#中的Bing Map API解析JSON代码,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4582字,纯文字阅读大概需要7分钟。
内容图文
![从C#中的Bing Map API解析JSON代码](/upload/InfoBanner/zyjiaocheng/758/39ee8e594fda484897d43fcbd4825838.jpg)
我使用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”:”Copyright © 2013 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.”,”resourceSets”:[{“estimatedTotal”:1,”resources”:[{“__type”:”Location:http://schemas.microsoft.com/search/local/ws/rest/v1″,”bbox”:[47.253395080566406,-123.16571807861328,47.946159362792969,-121.50344085693359],”name”:”Seattle, 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”:”Rooftop”,”usageTypes”:[“Display”]}],”matchCodes”:[“Good”]}]}],”statusCode”:200,”statusDescription”:”OK”,”traceId”:”08bee37ecb714d7cb7e2783eb8f873a4|LTSM000177|02.00.183.2300|LTSIPEVM000039″}
我现在的问题是:这是正确的方法吗?我如何获得数据?
我希望你能帮助我,我已经花了很多时间来解决这个问题.
– – -编辑 – – –
现在我使用JSON.net,但我不知道,如何解析我从网站获得的数据到字符串变量.
我尝试了以下方法:
var json = new WebClient().DownloadString("url");
但这不起作用,因为我正在开发Windows Phone.
System.Net.WebClient does not contain a definition for “DownloadString” …
任何人都可以帮助我使用Windows Phone应用程序.
解决方法:
使用json时,我更喜欢Json.Net.在声明必要的类之后,这段代码就是全部
var root = JsonConvert.DeserializeObject<RootObject>(json);
foreach (var rs in root.resourceSets)
{
foreach (var r in rs.resources)
{
Console.WriteLine(r.point.coordinates[0] + " , " + r.point.coordinates[1]);
}
}
您可以使用this site将您的json转换为具体类
public class Point
{
public string type { get; set; }
public List<double> coordinates { get; set; }
}
public class Address
{
public string adminDistrict { get; set; }
public string adminDistrict2 { get; set; }
public string countryRegion { get; set; }
public string formattedAddress { get; set; }
public string locality { get; set; }
}
public class GeocodePoint
{
public string type { get; set; }
public List<double> coordinates { get; set; }
public string calculationMethod { get; set; }
public List<string> usageTypes { get; set; }
}
public class Resource
{
public string __type { get; set; }
public List<double> bbox { get; set; }
public string name { get; set; }
public Point point { get; set; }
public Address address { get; set; }
public string confidence { get; set; }
public string entityType { get; set; }
public List<GeocodePoint> geocodePoints { get; set; }
public List<string> matchCodes { get; set; }
}
public class ResourceSet
{
public int estimatedTotal { get; set; }
public List<Resource> resources { get; set; }
}
public class RootObject
{
public string authenticationResultCode { get; set; }
public string brandLogoUri { get; set; }
public string copyright { get; set; }
public List<ResourceSet> resourceSets { get; set; }
public int statusCode { get; set; }
public string statusDescription { get; set; }
public string traceId { get; set; }
}
Json.net还允许您使用动态关键字而不声明任何这些类.
dynamic root = JsonConvert.DeserializeObject(json);
foreach (var rs in root.resourceSets)
{
foreach (var r in rs.resources)
{
Console.WriteLine(r.point.coordinates[0] + " , " + r.point.coordinates[1]);
}
}
内容总结
以上是互联网集市为您收集整理的从C#中的Bing Map API解析JSON代码全部内容,希望文章能够帮你解决从C#中的Bing Map API解析JSON代码所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。