我使用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; }
}
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中输入链接时,这是我的回复:
{"authenticationResultCode":"ValidCredentials","brandLogoUri":"http://dev.virtualearth.net/Branding/logo_powered_by.png","copyright":"Copyright©2013 Microsoft及其供应商.保留所有权利.未经Microsoft Corporation明确书面许可,不得访问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", "点":{ "类型": "点", "坐标":[47.603561401367188,-122.32943725585938]},"地址":{"adminDistrict":"WA","adminDistrict2":"King Co.","countryRegion":"United States","formattedAddress":"Seattle,WA","locality": "西雅图"}, "信心": "高", "的EntityType": "PopulatedPlace", "geocodePoints":[{ "类型": "点", "坐标":47.603561401367188,-122.32943725585938], "calculationMethod": "屋顶", "usageTypes":[ "显示"]}], "matchCodes":[ "好"]}]}], "的StatusCode":200, "状态说明": "OK", "traceId":"08bee37ecb714d7cb7e2783eb8f873a4 | LTSM000177 | 02.00.183.2300 | LTSIPEVM000039" }adminDistrict":"WA","adminDistrict2":"King Co.","countryRegion":"United States","formattedAddress":"Seattle,WA","locality":"Seattle"},"confidence":"高 " "的EntityType": "PopulatedPlace", "geocodePoints":[{ "类型": "点", "坐标":[47.603561401367188,-122.32943725585938], "calculationMethod": "屋顶", "usageTypes":["显示 "]}]," matchCodes ":[" 好 "]}]}]," 的StatusCode ":200," 状态说明 ":" OK", "traceId": "08bee37ecb714d7cb7e2783eb8f873a4 | LTSM000177 | 02.00.183.2300 | LTSIPEVM000039"}adminDistrict":"WA","adminDistrict2":"King Co.","countryRegion":"United States","formattedAddress":"Seattle,WA","locality":"Seattle"},"confidence":"高 " "的EntityType": "PopulatedPlace", "geocodePoints":[{ "类型": "点", "坐标":[47.603561401367188,-122.32943725585938], "calculationMethod": "屋顶", "usageTypes":["显示 "]}]," matchCodes ":[" 好 "]}]}]," 的StatusCode ":200," 状态说明 ":" OK", "traceId": "08bee37ecb714d7cb7e2783eb8f873a4 | LTSM000177 | 02.00.183.2300 | LTSIPEVM000039"}countryRegion":"United States","formattedAddress":"Seattle,WA","locality":"Seattle"},"confidence":"High","entityType":"PopulatedPlace","geocodePoints":[{"类型 ":" 点", "坐标":[47.603561401367188,-122.32943725585938], "calculationMethod": "屋顶", "usageTypes":[ "显示"]}], "matchCodes":[ "好"]}]} ] "的StatusCode":200, "状态说明": "OK", "traceId": "08bee37ecb714d7cb7e2783eb8f873a4 | LTSM000177 | 02.00.183.2300 | LTSIPEVM000039"}countryRegion":"United States","formattedAddress":"Seattle,WA","locality":"Seattle"},"confidence":"High","entityType":"PopulatedPlace","geocodePoints":[{"类型 ":" 点", "坐标":[47.603561401367188,-122.32943725585938], "calculationMethod": "屋顶", "usageTypes":[ "显示"]}], "matchCodes":[ "好"]}]} ] "的StatusCode":200, "状态说明": "OK", "traceId": "08bee37ecb714d7cb7e2783eb8f873a4 | LTSM000177 | 02.00.183.2300 | LTSIPEVM000039"}的EntityType ":" PopulatedPlace", "geocodePoints":[{ "类型": "点", "坐标":[47.603561401367188,-122.32943725585938], "calculationMethod": "屋顶", "usageTypes":[ "显示"]} ], "matchCodes":[ "好"]}]}], "的StatusCode":200, "状态说明": "OK", "traceId": "08bee37ecb714d7cb7e2783eb8f873a4 | LTSM000177 | 02.00.183.2300 | LTSIPEVM000039"}的EntityType ":" PopulatedPlace", "geocodePoints":[{ "类型": "点", "坐标":[47.603561401367188,-122.32943725585938], "calculationMethod": "屋顶", "usageTypes":[ "显示"]} ], "matchCodes":[ "好"]}]}], "的StatusCode":200, "状态说明": "OK", "traceId": "08bee37ecb714d7cb7e2783eb8f873a4 | LTSM000177 | 02.00.183.2300 | LTSIPEVM000039"}状态说明 ":" OK", "traceId": "08bee37ecb714d7cb7e2783eb8f873a4 | LTSM000177 | 02.00.183.2300 | LTSIPEVM000039"}状态说明 ":" OK", "traceId": "08bee37ecb714d7cb7e2783eb8f873a4 | LTSM000177 | 02.00.183.2300 | LTSIPEVM000039"}
我现在的问题是:这是正确的方法吗?我如何获得数据?
我希望你能帮助我,我已经花了很多时间来解决这个问题.
- - -编辑 - - -
现在我使用JSON.net,但我不知道,如何解析我从网站获得的数据到字符串变量.
我尝试了以下方法:
var json = new WebClient().DownloadString("url");
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为我正在开发Windows Phone.
System.Net.WebClient不包含"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]);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用此站点将您的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; }
}
Run Code Online (Sandbox Code Playgroud)
Json.net还允许您使用dynamic关键字而不声明任何这些类.
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]);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5239 次 |
| 最近记录: |