2017-06-16 4 views
0

Ich habe Klassen mit json2csharp generiert. Es ist alles gut in var content = response.Content; ich auch die Hauptklasse deserialisiert haben:Komplizierte Deserialisierung JSON mit geschachtelten Klassen C#

var responseData1 = JsonConvert.DeserializeObject<RootObjectChangeLocation>(content); 

Aber ich brauche lat und lng von dieser Klasse abzurufen:

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

Ich versuchte var responseData1 = JsonConvert.DeserializeObject<Location>(content); zu machen Koordinaten abrufen . Aber lat und lng in responseData1 sind leer, wenn ich es direkt mache. Wie kann ich sie bekommen? Dies wird Code generiert durch json2csharp:

public class AddressComponent 
    { 
     public string long_name { get; set; } 
     public string short_name { get; set; } 
     public List<string> types { get; set; } 
    } 

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

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

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

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

    public class Northeast2 
    { 
     public double lat { get; set; } 
     public double lng { get; set; } 
    } 

    public class Southwest2 
    { 
     public double lat { get; set; } 
     public double lng { get; set; } 
    } 

    public class Viewport 
    { 
     public Northeast2 northeast { get; set; } 
     public Southwest2 southwest { 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 Result 
    { 
     public List<AddressComponent> address_components { get; set; } 
     public string formatted_address { get; set; } 
     public Geometry geometry { get; set; } 
     public string place_id { get; set; } 
     public List<string> types { get; set; } 
    } 

    public class RootObjectChangeLocation 
    { 
     public List<Result> results { get; set; } 
     public string status { get; set; } 
    } 

Die jsonString ist:

{ 
    "results" : [ 
     { 
     "address_components" : [ 
      { 
       "long_name" : "Vinnytsia", 
       "short_name" : "Vinnytsia", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "Vinnyts'ka city council", 
       "short_name" : "Vinnyts'ka city council", 
       "types" : [ "administrative_area_level_3", "political" ] 
      }, 
      { 
       "long_name" : "Vinnyts'ka oblast", 
       "short_name" : "Vinnyts'ka oblast", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "Ukraine", 
       "short_name" : "UA", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "Vinnytsia, Vinnyts'ka oblast, Ukraine", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 49.27902, 
        "lng" : 28.5710879 
       }, 
       "southwest" : { 
        "lat" : 49.190448, 
        "lng" : 28.3681799 
       } 
      }, 
      "location" : { 
       "lat" : 49.233083, 
       "lng" : 28.468217 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 49.27902, 
        "lng" : 28.5710879 
       }, 
       "southwest" : { 
        "lat" : 49.1906116, 
        "lng" : 28.3681799 
       } 
      } 
     }, 
     "place_id" : "ChIJiWRaGWVbLUcR_nTd7lnh1Ms", 
     "types" : [ "locality", "political" ] 
     } 
    ], 
    "status" : "OK" 
} 
+0

Also ... was ist das Problem? –

+1

* Ich musste einige weitere Details hinzufügen, um diese Frage zu stellen ... * - dann bitte, bevor Sie die Frage stellen. Suchst du das? [Google Geocoding Json Parsing Problem in C#] (https://stackoverflow.com/q/28371365/3744182). – dbc

+1

Bitte teilen Sie die JSON, die Sie versuchen zu dekodieren. – dbc

Antwort

0

@dbc mir geholfen, die Lösung zu finden nach dieser link

 get_coordinates.Click += delegate 
     { 
      string city_val = change_location.Text; 
      var client = new RestClient("https://maps.googleapis.com/maps/api/geocode/json?address="); 
      var request = new RestRequest(city_val, Method.GET); 
      IRestResponse response = client.Execute(request); 
      var content = response.Content; 

      var responseData1 = JsonConvert.DeserializeObject<RootObjectChangeLocation>(content); 

      if (content == null||content=="") 
      { 
       Toast.MakeText(this, "City is empty or incorrect", ToastLength.Short).Show(); 
      } 
      else 
      { 
       foreach(var data in responseData1.results) 
       { 
        var lat = data.geometry.location.lat; 
       } 
      } 
      // Toast.MakeText(this, content, ToastLength.Long).Show(); 
      Console.WriteLine(content.ToString()); 
     }; 
Verwandte Themen