iviryxavyer
iviryxavyer

Reputation: 131

Get values from string response c#

This json is from a HTTPresponse from google API:

{
  "results": [
    {
      "address_components": [
        {
          "long_name": "Europe",
          "short_name": "Europe",
          "types": [
            "continent",
            "establishment",
            "natural_feature"
          ]
        }
      ],
      "formatted_address": "Europe",
      "geometry": {
        "bounds": {
          "northeast": {
            "lat": 82.1673907,
            "lng": 74.3555001
          },
          "southwest": {
            "lat": 34.5428,
            "lng": -31.4647999
          }
        },
        "location": {
          "lat": 54.5259614,
          "lng": 15.2551187
        },
        "location_type": "APPROXIMATE",
        "viewport": {
          "northeast": {
            "lat": 65,
            "lng": 55
          },
          "southwest": {
            "lat": 34,
            "lng": -11
          }
        }
      },
      "place_id": "ChIJhdqtz4aI7UYRefD8s-aZ73I",
      "types": [
        "continent",
        "establishment",
        "natural_feature"
      ]
    }
  ],
  "status": "OK"
}

I built a list class:

public class geocode {
    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 int lat { get; set; }
        public int lng { get; set; }
    }

    public class Southwest2
    {
        public int lat { get; set; }
        public int 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 RootObject
    {
        public List<Result> results { get; set; }
        public string status { get; set; }
    }
}

And this is far I get:

nombrePapa = nombrePapa.ToUpper();
var resultadoGeocoding = ggo.geocoding(nombrePapa);

//var result = JsonConvert.DeserializeObject<listas.geocode.Result>(resultadoGeocoding);

List<listas.geocode> lista = JsonConvert.DeserializeObject<List<listas.geocode>>(resultadoGeocoding);

But this deserialization stuck me on:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Models.listas+geocode]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

I'm missing something?

Upvotes: 0

Views: 1616

Answers (1)

steliosbl
steliosbl

Reputation: 8921

What the error is telling you is that in order to correctly deserialize your JSON into a List<listas.geocode>, you'd need your JSON to represent an array. What you are giving the deserializer instead is an object, with two properties, results and status.

I'm guessing you used json2csharp to get your classes, since you already have a class for the root object. You just forgot to use it

To deserialize the response you need to do:

var response = JsonConvert.DeserializeObject<RootObject>(resultadoGeocoding);
var lista = response.Results;

Additional note: For status, you can use a string (easiest option), or you can use an enum as detailed here.

Upvotes: 1

Related Questions