VB.NET JSON反序列化报错:无法将JSON对象转为List`1类型求助
错误原因分析
- 反序列化目标类型不匹配:返回的JSON是一个顶层对象(
FeatureCollection),而非直接的properties数组。你当前尝试将整个JSON对象反序列化为List(Of properties),必然导致类型不匹配错误。 - 实体模型结构错误:
AutoCompleteResponse中的features被定义为String,但JSON里features是对象数组,每个元素包含properties字段;- 模型中
AutoCompleteResponse的Properties属性在JSON中不存在,完全对应错误; - 部分嵌套对象(如
datasource、rank、timezone)被定义为String,无法正确映射JSON中的嵌套结构。
修正方案
1. 修正实体模型
根据JSON结构,定义完整的对应类:
Public Class AutoCompleteResponse Public Property type As String Public Property features As List(Of Feature) Public Property query As Query End Class Public Class Feature Public Property type As String Public Property geometry As Geometry Public Property properties As Properties End Class Public Class Geometry Public Property type As String Public Property coordinates As List(Of Double) End Class Public Class Properties Public Property country_code As String Public Property housenumber As String Public Property street As String Public Property country As String Public Property county As String Public Property datasource As Datasource Public Property state As String Public Property district As String Public Property city As String Public Property lon As Double Public Property lat As Double Public Property postcode As String Public Property formatted As String Public Property address_line1 As String Public Property address_line2 As String Public Property timezone As Timezone Public Property plus_code As String Public Property plus_code_short As String Public Property result_type As String Public Property rank As Rank Public Property place_id As String Public Property county_code As String End Class Public Class Datasource Public Property sourcename As String Public Property attribution As String Public Property license As String End Class Public Class Timezone Public Property name As String Public Property offset_STD As String Public Property offset_STD_seconds As Integer Public Property offset_DST As String Public Property offset_DST_seconds As Integer Public Property abbreviation_STD As String Public Property abbreviation_DST As String End Class Public Class Rank Public Property popularity As Double Public Property confidence As Double Public Property match_type As String End Class Public Class Query Public Property text As String Public Property parsed As Parsed End Class Public Class Parsed Public Property housenumber As String Public Property street As String Public Property expected_type As String End Class
2. 修正反序列化代码
先反序列化为AutoCompleteResponse对象,再从中提取properties列表:
' 反序列化顶层对象 Dim responseObj As AutoCompleteResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(Of AutoCompleteResponse)(response) ' 提取所有properties Dim test As List(Of Properties) = responseObj.features.Select(Function(f) f.properties).ToList() e.Result = test
额外说明
- 保持类名和属性名与JSON字段一致,避免潜在的大小写匹配问题;
- 数值类型修正:JSON中的
lon、lat、popularity等是数值类型,模型中对应为Double而非String,确保数据类型匹配; - 完整映射嵌套结构:所有JSON中的嵌套对象都需要对应定义类,否则会导致反序列化失败或数据丢失。
内容的提问来源于stack exchange,提问作者August Christopher Paras
相关产品推荐
相关产品推荐

