Visual Basic 获取 Json 值
Visual Basic Get Json Value
我正在制作一个应用程序,我需要从 json 文件中读取某个值。
json文件格式如下...
{
"files": [
{
"id": [
6856,
3667
],
"uid": 15749645081288,
"file_id": 6856,
"name": "Name",
"version": "001",
"category_id": 4,
"category_name": "OLD_VERSION",
"is_primary": false,
"size": 4,
"file_name": "Name_001-1539-001-1633022640.zip",
"uploaded_timestamp": 1633022640,
"uploaded_time": "2021-09-30T17:24:00.000+00:00",
"mod_version": "001",
"external_virus_scan_url": "https://www.virustotal.com",
"description": "Version 001 Public",
"size_kb": 4,
"size_in_bytes": 3961,
"changelog_html": "Public",
"content_preview_link": "URL"
},
{
"id": [
6872,
3667
],
"uid": 15749645081304,
"file_id": 6872,
"name": "Name_002",
"version": "002",
"category_id": 1,
"category_name": "MAIN",
"is_primary": true,
"size": 4,
"file_name": "Name_002-1539-002-1633106391.zip",
"uploaded_timestamp": 1633106391,
"uploaded_time": "2021-10-01T16:39:51.000+00:00",
"mod_version": "002",
"external_virus_scan_url": "https://www.virustotal.com/",
"description": "Update 002 . See changelog.",
"size_kb": 4,
"size_in_bytes": 4150,
"changelog_html": "Stuff",
"content_preview_link": "URL"
}
],
"file_updates": [
{
"old_file_id": 6856,
"new_file_id": 6872,
"old_file_name": "Name_001-1539-001-1633022640.zip",
"new_file_name": "Name_002-1539-002-1633106391.zip",
"uploaded_timestamp": 1633106392,
"uploaded_time": "2021-10-01T16:39:52.000+00:00"
}
]
}
我需要“file_id”值
我的代码如下
Dim json As String = ModLinkData(n)
Dim read = Newtonsoft.Json.Linq.JObject.Parse(json)
Dim FileID As String = read.Item("files").ToString
这将正确地为我提供“文件”下的所有内容。我不知道如何获取 file_id 值。当我将“文件”更改为
我喜欢为此使用强类型,所以我会研究 JSON 序列化
您可以通过将 json 字符串复制到剪贴板在 .NET 中创建 class,然后
In Visual Studio put the cursor where you want the new classes
Edit >> Paste Special >> Paste JSON as Classes
结果是这样的(我已经在 Rootobject 中将一些数组类型更改为列表)
Public Class Rootobject
Public Property files As List(Of File)
Public Property file_updates As List(Of File_Updates)
End Class
Public Class File
Public Property id As Integer()
Public Property uid As Long
Public Property file_id As Integer
Public Property name As String
Public Property version As String
Public Property category_id As Integer
Public Property category_name As String
Public Property is_primary As Boolean
Public Property size As Integer
Public Property file_name As String
Public Property uploaded_timestamp As Integer
Public Property uploaded_time As Date
Public Property mod_version As String
Public Property external_virus_scan_url As String
Public Property description As String
Public Property size_kb As Integer
Public Property size_in_bytes As Integer
Public Property changelog_html As String
Public Property content_preview_link As String
End Class
Public Class File_Updates
Public Property old_file_id As Integer
Public Property new_file_id As Integer
Public Property old_file_name As String
Public Property new_file_name As String
Public Property uploaded_timestamp As Integer
Public Property uploaded_time As Date
End Class
既然你已经引用了NewtonSoft.Json,我们可以使用其中的反序列化方法
Dim json = ModLinkData(n)
Dim rootobject = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Rootobject)(json)
Dim fileIDs = rootobject.files.First().id ' this is an array
Dim fileID1 = fileIDs(0)
Dim fileID2 = fileIDs(1)
Console.WriteLine(fileID1)
Console.WriteLine(fileID2)
输出
6856
3667
如果您在设计时就知道数据的设计,那么这种方法优于您的方法,因为您获得了强类型化。比较
Dim FileID = read.Item("files")
到
Dim fileIDs = rootobject.files.First().id
并且在我的示例中看到 files
是对象的具体 属性,而在您的示例中 files
是用于索引 属性 的可变字符串其中returns一个JToken
无论如何,我认为您遇到的主要问题是在返回数组时期望单个值,这在任何一种情况下都是一个问题,但是对于序列化,编译器会通知您而不是 运行-时间。
我正在制作一个应用程序,我需要从 json 文件中读取某个值。
json文件格式如下...
{
"files": [
{
"id": [
6856,
3667
],
"uid": 15749645081288,
"file_id": 6856,
"name": "Name",
"version": "001",
"category_id": 4,
"category_name": "OLD_VERSION",
"is_primary": false,
"size": 4,
"file_name": "Name_001-1539-001-1633022640.zip",
"uploaded_timestamp": 1633022640,
"uploaded_time": "2021-09-30T17:24:00.000+00:00",
"mod_version": "001",
"external_virus_scan_url": "https://www.virustotal.com",
"description": "Version 001 Public",
"size_kb": 4,
"size_in_bytes": 3961,
"changelog_html": "Public",
"content_preview_link": "URL"
},
{
"id": [
6872,
3667
],
"uid": 15749645081304,
"file_id": 6872,
"name": "Name_002",
"version": "002",
"category_id": 1,
"category_name": "MAIN",
"is_primary": true,
"size": 4,
"file_name": "Name_002-1539-002-1633106391.zip",
"uploaded_timestamp": 1633106391,
"uploaded_time": "2021-10-01T16:39:51.000+00:00",
"mod_version": "002",
"external_virus_scan_url": "https://www.virustotal.com/",
"description": "Update 002 . See changelog.",
"size_kb": 4,
"size_in_bytes": 4150,
"changelog_html": "Stuff",
"content_preview_link": "URL"
}
],
"file_updates": [
{
"old_file_id": 6856,
"new_file_id": 6872,
"old_file_name": "Name_001-1539-001-1633022640.zip",
"new_file_name": "Name_002-1539-002-1633106391.zip",
"uploaded_timestamp": 1633106392,
"uploaded_time": "2021-10-01T16:39:52.000+00:00"
}
]
}
我需要“file_id”值
我的代码如下
Dim json As String = ModLinkData(n)
Dim read = Newtonsoft.Json.Linq.JObject.Parse(json)
Dim FileID As String = read.Item("files").ToString
这将正确地为我提供“文件”下的所有内容。我不知道如何获取 file_id 值。当我将“文件”更改为
我喜欢为此使用强类型,所以我会研究 JSON 序列化
您可以通过将 json 字符串复制到剪贴板在 .NET 中创建 class,然后
In Visual Studio put the cursor where you want the new classes
Edit >> Paste Special >> Paste JSON as Classes
结果是这样的(我已经在 Rootobject 中将一些数组类型更改为列表)
Public Class Rootobject
Public Property files As List(Of File)
Public Property file_updates As List(Of File_Updates)
End Class
Public Class File
Public Property id As Integer()
Public Property uid As Long
Public Property file_id As Integer
Public Property name As String
Public Property version As String
Public Property category_id As Integer
Public Property category_name As String
Public Property is_primary As Boolean
Public Property size As Integer
Public Property file_name As String
Public Property uploaded_timestamp As Integer
Public Property uploaded_time As Date
Public Property mod_version As String
Public Property external_virus_scan_url As String
Public Property description As String
Public Property size_kb As Integer
Public Property size_in_bytes As Integer
Public Property changelog_html As String
Public Property content_preview_link As String
End Class
Public Class File_Updates
Public Property old_file_id As Integer
Public Property new_file_id As Integer
Public Property old_file_name As String
Public Property new_file_name As String
Public Property uploaded_timestamp As Integer
Public Property uploaded_time As Date
End Class
既然你已经引用了NewtonSoft.Json,我们可以使用其中的反序列化方法
Dim json = ModLinkData(n)
Dim rootobject = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Rootobject)(json)
Dim fileIDs = rootobject.files.First().id ' this is an array
Dim fileID1 = fileIDs(0)
Dim fileID2 = fileIDs(1)
Console.WriteLine(fileID1)
Console.WriteLine(fileID2)
输出
6856
3667
如果您在设计时就知道数据的设计,那么这种方法优于您的方法,因为您获得了强类型化。比较
Dim FileID = read.Item("files")
到
Dim fileIDs = rootobject.files.First().id
并且在我的示例中看到 files
是对象的具体 属性,而在您的示例中 files
是用于索引 属性 的可变字符串其中returns一个JToken
无论如何,我认为您遇到的主要问题是在返回数组时期望单个值,这在任何一种情况下都是一个问题,但是对于序列化,编译器会通知您而不是 运行-时间。