简体   繁体   English

如何从C#ASP.NET中的JSON字符串中仅解析纬度和经度信息?

[英]How can I parse just the latitude and longitude information from a JSON string in C# ASP.NET?

I've looked everywhere and can't find a solution that works for me with this problem. 我到处都是,找不到解决此问题的解决方案。

I'm using the Google Maps API and need to take an address, convert it to latitude/longitude, and then generate my map with those values. 我正在使用Google Maps API,需要输入一个地址,将其转换为纬度/经度,然后使用这些值生成我的地图。

I'm using NewtonSoft.Json to try and deserialize my string but am getting different errors for every method I try. 我正在使用NewtonSoft.Json尝试对字符串进行反序列化,但是我尝试的每种方法都会遇到不同的错误。

Here is how I'm accessing my JSON: 这是我访问JSON的方式:

var webClient = new WebClient();
var json = webClient.DownloadString(@"https://maps.googleapis.com/maps/api/geocode/json?address=1600%20Pennsylvania%20Ave%20NW,%20Washington,%20DC%2020500&key=AIzaSyAKORLziVDRpaIlRs7NrPwGsye9NNn6Mdw");

Right now I'm only using a static address for debugging purposes, but will be switching to variables later. 现在,我仅将静态地址用于调试目的,但是稍后将切换至变量。

Here is the URL to see the JSON: 这是查看JSON的URL:

I know that this is imported correctly because I'm using System.Diagnostics.Debug.WriteLine("json "+json); 我知道这是正确导入的,因为我正在使用System.Diagnostics.Debug.WriteLine("json "+json); to check and debug. 检查和调试。

I've also confirmed with https://jsonlint.com/ that it is valid JSON. 我还通过https://jsonlint.com/确认了它是有效的JSON。

So now, I need to deserialize this String to grab 所以现在,我需要反序列化此String才能抓取

  "location" : { "lat" : 38.8976633, "lng" : -77.03657389999999 

and save them into variables for later access. 并将它们保存到变量中以供以后访问。

I would like to do this without making an entire class, but if that's what needs to be done I can manage. 我想不参加整个课程就这样做,但是如果那是我可以做的,那就可以管理。

Can anyone help me format the JsonConvert.DeserializeObject() command to work for this? 谁能帮我格式化JsonConvert.DeserializeObject()命令来解决这个问题?

You can use JSON.net to deserialize partial JSON fragments here is an example to what you are trying to achieve, and this is the code for your case. 您可以使用JSON.net来反序列化部分JSON片段, 是您要实现的示例,这是您的案例的代码。

 var webClient = new WebClient();
 string json = webClient.DownloadString(@"https://maps.googleapis.com/maps/api/geocode/json?address=1600%20Pennsylvania%20Ave%20NW,%20Washington,%20DC%2020500&key=AIzaSyAKORLziVDRpaIlRs7NrPwGsye9NNn6Mdw");
 JObject jsonOb = JObject.Parse(json);
 double lat  = jsonOb["results"]["geometry"]["location"]["lat"];
 double lng  = jsonOb["results"]["geometry"]["location"]["lng"];

First of all you need to define c# classes to your json. 首先,您需要为json定义c#类。 It is not manual work. 这不是手动工作。 Copy all json to clipboard. 将所有json复制到剪贴板。 Then go to Visual Studio, open create new .cs file with class, and then Edit->Paster special->Paste JSON as Classes . 然后转到Visual Studio,打开创建带有类的新.cs文件,然后打开Edit->Paster special->Paste JSON as Classes In your case it will be these classes 您的情况将是这些课程

public class Rootobject
{
    public Result[] results { get; set; }
    public string status { get; set; }
}

public class Result
{
    public Address_Components[] address_components { get; set; }
    public string formatted_address { get; set; }
    public Geometry geometry { get; set; }
    public string place_id { get; set; }
    public string[] types { get; set; }
}

public class Geometry
{
    public Bounds bounds { get; set; }
    public Location location { get; set; }
    public string location_type { get; set; }
    public Viewport viewport { get; set; }
}

public class Bounds
{
    public Northeast northeast { get; set; }
    public Southwest southwest { get; set; }
}

public class Northeast
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Southwest
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Location
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Viewport
{
    public Northeast1 northeast { get; set; }
    public Southwest1 southwest { get; set; }
}

public class Northeast1
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Southwest1
{
    public float lat { get; set; }
    public float lng { get; set; }
}

public class Address_Components
{
    public string long_name { get; set; }
    public string short_name { get; set; }
    public string[] types { get; set; }
}

Then you can desesrialize it based on this classes 然后您可以基于此类对它进行反序列化

 var webClient = new WebClient();
 var json = webClient.DownloadString(@"https://maps.googleapis.com/maps/api/geocode/json?address=1600%20Pennsylvania%20Ave%20NW,%20Washington,%20DC%2020500&key=AIzaSyAKORLziVDRpaIlRs7NrPwGsye9NNn6Mdw");
 var parsedObj = JsonConvert.DeserializeObject<Rootobject>(json);
 var location = parsedObj.results?[0].geometry?.location;
 Console.WriteLine(location.lat);
 Console.WriteLine(location.lng);

It would be best to make an object to cast the JSON into. 最好使一个对象将JSON转换为。 But you could use untyped objects. 但是您可以使用无类型的对象。

var outputObject = JsonConvert.DeserializeObject(JsonString)

when you make an object for the convert to cast to, you do the following: 为转换对象转换为对象时,请执行以下操作:

Type outputObject = JsonConvert.DeserializeObject<Type>(jsonstring);

This worked for me 这对我有用

var webClient = new WebClient();
var json = webClient.DownloadString(@"https://maps.googleapis.com/maps/api/geocode/json?address=1600%20Pennsylvania%20Ave%20NW,%20Washington,%20DC%2020500&key=AIzaSyAKORLziVDRpaIlRs7NrPwGsye9NNn6Mdw");
var obj = JsonConvert.DeserializeObject<dynamic>(json);
Console.WriteLine(obj.results[0].geometry.bounds.northeast.lat.Value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Asp.net MVC 和 C# 从 SQL Server 搜索基于纬度和经度的记录 - How to search Latitude and Longitude based record from SQL Server use Asp.net MVC and C# C#在asp.net应用程序中获取当前纬度和经度 - C# Get current latitude and longitude in asp.net application 如何在asp.net中通过文本框设置经度和纬度位置? - How can I set Longitude and Latitude Positions by textboxes in asp.net? How to parse SQL JSON string in C# in asp.net mvc web api? - How to parse SQL JSON string in C# in asp.net mvc web api? 我如何从udp数据包中获取经度和纬度值C# - How can i get latitude and longitude values from udp packets c# 如何在ASP.NET MVC中解析JSON? - How can i parse JSON in ASP.NET MVC? 如何使用C#在ASP.Net中将Json字符串转换为DataTable - How to Convert Json String into DataTable in ASP.Net with C# 如何在asp.net c#中验证json字符串? - how to validate the json string in asp.net c#? 通过c# webforms asp.net中的google map api从下拉列表中的选定城市获取textbox1和textbox2的纬度和经度 - Get latitude and longitude to textbox1 and textbox2 from selected city in dropdownlist through google map api in c# webforms asp.net 如何从 C# Asp.Net 中的 JSON 结果字符串中检索值 - How To Retrieve Value From JSON Result String in C# Asp.Net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM