使用Json.NET解析Google Maps API返回的结果 [英] Using Json.NET to parse result returned by Google Maps API

查看:80
本文介绍了使用Json.NET解析Google Maps API返回的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用google map api的网络服务发出Web请求,并获取json字符串,然后获取输入地址所需的纬度和经度.

I am trying to use google map api's web service to make a web request, and get the json string, and then get the latitude and longitude I need for the input address.

一切都很好.我得到了我需要的json字符串.

Everything is fine. I got the json string I need.

现在我正在使用Json.net来解析字符串.

Now I am using Json.net to parse the string.

我不知道为什么,但是我根本无法将其转换为JArray.

I don't know why, but I simply cannot convert it into a JArray.

这是json字符串

Here is the json string

有人可以教我如何编写c#代码以在geometry > location中获得latlng吗?

Can anyone teach me how to write the c# code to get the lat and lng in geometry > location?

谢谢

这是我的代码和错误屏幕截图

Here is my codes and the bug screenshot

推荐答案

使用JSON.NET解析JSON时,您有一些选择.

You have a few options when using JSON.NET to Parse the JSON.

最好的选择,恕我直言,是使用序列化将对象拉回到结构化类型,您可以像处理其他任何类一样操作该结构化类型.为此,您可以在JSON.NET文档中看到序列化(我可以如果还不够清晰,还可以发布更多详细信息.)

The best option, IMHO, is to use Serialization to pull the object back into a structured type that you can manipulate as you could any other class. For this you can see serialization in the JSON.NET documentation (I can also post more details if that isn't clear enough).

如果只想获取地址,如您在问题中列出的那样,您还可以使用LINQ功能将信息拉回.您可以使用类似于以下代码的代码来实现它(关键在于 SelectToken 方法提取所需的详细信息).

If all you want is to grab the address, as you listed in your question, you can also use the LINQ feature to pull that information back. You could use code similar to the following to pull it off (the key lies in the SelectToken method to pull back the details you need).

    Dim json As Newtonsoft.Json.Linq.JObject
    json = Newtonsoft.Json.Linq.JObject.Parse(jsonString)
    json.SelectToken("results.formatted_address").ToString()

您还可以使用Linq的所有常规功能遍历JSON.也请参阅LINQ文档.

You can also use all the normal power of Linq to traverse the JSON as you'd expect. See the LINQ documentation as well.

这篇关于使用Json.NET解析Google Maps API返回的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆