System.Text.Json.JsonException:'JSON 值无法转换为 System.Collections.Generic.List`1

问题描述 投票:0回答:1

我正在尝试构建一个 WPF 应用程序来将假日详细信息存储在 SQL Server 数据库中。我向最终用户请求地址并尝试对地址进行地理编码。为此,我从代码发出一个 Web 请求,并尝试反序列化我收到的 Json 响应。 HTTP 调用成功并返回预期的格式化 Json 响应,但对我的类的反序列化失败。 来自代码的 WebRequest

s = WebRequest.CreateHttp("https://maps.googleapis.com/maps/api/geocode/json" & postdata)

尝试反序列化收到的 Json 响应,如下所示:-

{
    "results": [
        {
            "address_components": [
                {
                    "long_name": "6",
                    "short_name": "6",
                    "types": [
                        "street_number"
                    ]
                },
                {
                    "long_name": "Clitheroe Road",
                    "short_name": "Clitheroe Rd",
                    "types": [
                        "route"
                    ]
                },
                {
                    "long_name": "Lytham St Annes",
                    "short_name": "Lytham St Annes",
                    "types": [
                        "locality",
                        "political"
                    ]
                },
                {
                    "long_name": "Lytham Saint Annes",
                    "short_name": "Lytham Saint Annes",
                    "types": [
                        "postal_town"
                    ]
                },
                {
                    "long_name": "Lancashire",
                    "short_name": "Lancashire",
                    "types": [
                        "administrative_area_level_2",
                        "political"
                    ]
                },
                {
                    "long_name": "England",
                    "short_name": "England",
                    "types": [
                        "administrative_area_level_1",
                        "political"
                    ]
                },
                {
                    "long_name": "United Kingdom",
                    "short_name": "GB",
                    "types": [
                        "country",
                        "political"
                    ]
                },
                {
                    "long_name": "FY8 3QN",
                    "short_name": "FY8 3QN",
                    "types": [
                        "postal_code"
                    ]
                }
            ],
            "formatted_address": "6 Clitheroe Rd, Lytham St Annes, Lytham Saint Annes FY8 3QN, UK",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 53.752827,
                        "lng": -3.0009766
                    },
                    "southwest": {
                        "lat": 53.7526356,
                        "lng": -3.0012806
                    }
                },
                "location": {
                    "lat": 53.7527275,
                    "lng": -3.0011181
                },
                "location_type": "ROOFTOP",
                "viewport": {
                    "northeast": {
                        "lat": 53.7540894302915,
                        "lng": -2.999763319708498
                    },
                    "southwest": {
                        "lat": 53.7513914697085,
                        "lng": -3.002461280291502
                    }
                }
            },
            "place_id": "ChIJTyNBERpBe0gRgk37kcNrmdA",
            "types": [
                "premise"
            ]
        }
    ],
    "status": "OK"
}

具有以下反序列化类:-

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class AddressComponent
{
    public string long_name { get; set; }
    public string short_name { get; set; }
    public List<string> types { get; set; }
}
public class Bounds
{
    public Northeast northeast { get; set; }
    public Southwest 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 Location
{
    public double lat { get; set; }
    public double lng { get; set; }
}
public class Northeast
{
    public double lat { get; set; }
    public double lng { 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 Root
{
    public List<Result> results { get; set; }
    public string status { get; set; }
}
public class Southwest
{
    public double lat { get; set; }
    public double lng { get; set; }
}
public class Viewport
{
    public Northeast northeast { get; set; }
    public Southwest southwest { get; set; }
}

出现以下错误:-

 System.Text.Json.JsonException:
  'The JSON value could not be converted to 
 System.Collections.Generic.List`1[Holiday.GetAddress+AddressComponent]. 
 Path: $.results[0].address_components[0] | LineNumber: 6 | BytePositionInLine: 13.'

四处寻找提示和线索表明解串器只是期待 即使 Json 结果显示一个数组,也只有一个“结果”。

c# google-geocoding-api jsondecodeerror
1个回答
0
投票

在堆栈溢出中找到一个链接,建议我使用“编辑”-“选择性粘贴”-“在 Visual Studio 中将 Json 粘贴为类”来生成我的类结构。这给了我以下内容:

Public Class Rootobject
    Public results() As Result
    Public status As String
End Class

Public Class Result
    Public address_components() As Address_Components
    Public formatted_address As String
    Public geometry As Geometry
    Public place_id As String
    Public types() As String
End Class

Public Class Geometry
    Public bounds As Bounds
    Public location As Location
    Public location_type As String
    Public viewport As Viewport
End Class

Public Class Bounds
    Public northeast As Northeast
    Public southwest As Southwest
End Class

Public Class Northeast
    Public lat As Single
    Public lng As Single
End Class

Public Class Southwest
    Public lat As Single
    Public lng As Single
End Class

Public Class Location
    Public lat As Single
    Public lng As Single
End Class

Public Class Viewport
    Public northeast As Northeast1
    Public southwest As Southwest1
End Class

Public Class Northeast1
    Public lat As Single
    Public lng As Single
End Class

Public Class Southwest1
    Public lat As Single
    Public lng As Single
End Class

Public Class Address_Components
    Public long_name As String
    Public short_name As String
    Public types() As String
End Class

results() 现在给出结果,而不是由 https://json2csharp.com/

生成的结果列表(结果列表)

代码现在可以反序列化 json 响应,无需任何代码修改。

© www.soinside.com 2019 - 2024. All rights reserved.