从字符串响应中获取值 c#

Get values from string response c#

此 json 来自 google API 的 HTTP 响应:

{
  "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"
}

我建立了一个列表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; }
    }
}

这就是我得到的:

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);

但是这个反序列化让我陷入困境:

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.

我错过了什么?

错误告诉您的是,为了将 JSON 正确反序列化为 List<listas.geocode>,您需要 JSON 来表示数组。你给反序列化器的是一个 object,有两个属性,resultsstatus.

我猜你用 json2csharp 得到了你的 classes,因为你 已经有一个 class 作为根对象 .你只是忘了使用它

要反序列化您需要做的响应:

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

附加说明:对于 status,您可以使用字符串(最简单的选项),也可以使用枚举 here