中国建设人才服务信息网站,做网站哪家网站好,郴州网站优化,html网页设计案例和代码在VB.NET中#xff0c;若要从 JSON 数据里提取Data.DataList数组中的CategoryId#xff0c;并将其转换为VB.NET数组#xff0c;可借助Json.NET#xff08;Newtonsoft.Json#xff09;库来实现。下面为你详细介绍具体的实现步骤和代码示例#xff1a;
一、实现 JSON 到数…在VB.NET中若要从 JSON 数据里提取Data.DataList数组中的CategoryId并将其转换为VB.NET数组可借助Json.NETNewtonsoft.Json库来实现。下面为你详细介绍具体的实现步骤和代码示例
一、实现 JSON 到数组的转换
方法 1通过 JObject 解析灵活通用
Imports Newtonsoft.Json
Imports Newtonsoft.Json.LinqPublic Function ExtractCategoryIds(jsonText As String) As Long()Try 解析JSON根对象Dim jsonObj As JObject JObject.Parse(jsonText) 获取DataList数组Dim dataList As JArray jsonObj?(Data)?(DataList) as JArrayIf dataList Is Nothing ThenReturn New Long(-1) {} 返回空数组End If 提取CategoryId并转换为数组Return dataList.Select(Function(item) item?(CategoryId)?.Value(Of Long)()).Where(Function(id) id.HasValue).Select(Function(id) id.Value).ToArray()Catch ex As ExceptionConsole.WriteLine(JSON解析错误: ex.Message)Return New Long(-1) {} 出错时返回空数组End Try
End Function方法 2反序列化为强类型对象类型安全
Imports Newtonsoft.Json 定义数据模型
Public Class RootObjectPublic Property Code As IntegerPublic Property Data As DataObject
End ClassPublic Class DataObjectPublic Property TotalCount As IntegerPublic Property PageIndex As IntegerPublic Property PageSize As IntegerPublic Property DataList As List(Of CategoryItem)
End ClassPublic Class CategoryItemPublic Property CategoryId As Long
End Class 转换方法
Public Function ExtractCategoryIdsWithModel(jsonText As String) As Long()Try 反序列化为强类型对象Dim root As RootObject JsonConvert.DeserializeObject(Of RootObject)(jsonText) 提取CategoryId数组If root?.Data?.DataList Is Nothing ThenReturn New Long(-1) {}End IfReturn root.Data.DataList.Select(Function(item) item.CategoryId).ToArray()Catch ex As ExceptionConsole.WriteLine(JSON反序列化错误: ex.Message)Return New Long(-1) {}End Try
End Function二、使用示例
Sub Main()Dim jsonText As String {Code:200,Data:{TotalCount:14,PageIndex:1,PageSize:100,DataList:[{CategoryId:271},{CategoryId:272}]}} 方法1动态解析Dim categoryIds1 As Long() ExtractCategoryIds(jsonText) 方法2强类型解析Dim categoryIds2 As Long() ExtractCategoryIdsWithModel(jsonText) 输出结果Console.WriteLine(提取的CategoryId数组:)For Each id In categoryIds1Console.WriteLine(id) 输出: 271, 272Next
End Sub三、关键要点说明 空值处理 运用?.操作符来避免出现NullReferenceException。对可能为空的数组进行检查防止程序崩溃。 类型转换 采用Value(Of Long)()方法将 JSON 值转换为 Long 类型。借助Where(Function(id) id.HasValue)过滤掉无效的值。 异常处理 捕获JsonReaderException和JsonSerializationException增强程序的健壮性。出错时返回空数组而非Nothing减少上层代码的判断逻辑。
四、扩展功能
1. 处理嵌套路径通用方法
vb
Public Function ExtractValuesByPath(jsonText As String, path As String) As Long()TryDim jsonToken As JToken JToken.Parse(jsonText)Dim tokens As IEnumerable(Of JToken) jsonToken.SelectTokens(path)Return tokens.Select(Function(token) token.Value(Of Long)()).ToArray()Catch ex As ExceptionConsole.WriteLine($提取路径 {path} 出错: {ex.Message})Return New Long(-1) {}End Try
End Function 使用示例
Dim categoryIds As Long() ExtractValuesByPath(jsonText, $.Data.DataList[*].CategoryId)2. 异步解析大 JSON 文件
Public Async Function ExtractCategoryIdsAsync(jsonText As String) As Task(Of Long())TryUsing reader As New StringReader(jsonText)Using jsonReader As New JsonTextReader(reader)Dim serializer As JsonSerializer New JsonSerializer()Dim root As RootObject Await Task.Run(Function() serializer.Deserialize(Of RootObject)(jsonReader)End Function)Return root?.Data?.DataList?.Select(Function(item) item.CategoryId).ToArray() ?? New Long(-1) {}End UsingEnd UsingCatch ex As ExceptionConsole.WriteLine(异步解析错误: ex.Message)Return New Long(-1) {}End Try
End Function五、性能考量
小 JSON推荐使用强类型反序列化方法 2这样能提高代码的可读性和类型安全性。大 JSON建议采用JsonTextReader进行流式解析以降低内存的占用。动态路径可使用SelectTokens方法来处理复杂的 JSON 结构。
通过上述方法你可以在VB.NET中高效、安全地从 JSON 数据里提取所需的数组。
在不使用第三方库的情况下如何实现JSON到数组的转换
提供一些关于JSON和数组操作的VB.NET最佳实践。
除了Json.NET库还有哪些常用的JSON库可用于VB.NET