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序列化

你可以在。net中通过复制json字符串到剪贴板来创建一个类,然后

在Visual Studio中,将光标放在需要新建类的位置
编辑>>膏体专用>>将JSON粘贴为类

结果是这样的(我已经改变了一些数组类型的列表在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是一个变量字符串,你用来索引一个返回JToken

的属性无论如何,我认为你主要有一个问题,期望一个单一的值,当一个数组被返回,这将是一个问题,在任何情况下,但序列化你是由编译器通知,而不是在运行时。

最新更新