简体   繁体   中英

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.

I'm using NewtonSoft.Json to try and deserialize my string but am getting different errors for every method I try.

Here is how I'm accessing my 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:

I know that this is imported correctly because I'm using System.Diagnostics.Debug.WriteLine("json "+json); to check and debug.

I've also confirmed with https://jsonlint.com/ that it is valid JSON.

So now, I need to deserialize this String to grab

  "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?

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.

 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. It is not manual work. Copy all json to clipboard. Then go to Visual Studio, open create new .cs file with class, and then 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. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM