I am making an application and I need to read a certain value from a json file.
The json file is formatted as follows...
{
"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"
}
]
}
And I am in need of the "file_id" Value
The Code I have is as follows
Dim json As String = ModLinkData(n)
Dim read = Newtonsoft.Json.Linq.JObject.Parse(json)
Dim FileID As String = read.Item("files").ToString
This will properly give me everything Under "files". I can not figure out how to ger the file_id value. When I change "files" to
CodePudding user response:
I like to use strong typing for this, so I would look into JSON Serialization
You can create a class in .NET by copying your json string to the clipboard, then
In Visual Studio put the cursor where you want the new classes
Edit >> Paste Special >> Paste JSON as Classes
And the result is this (I have changed some array types to Lists in 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
Since you are already referencing NewtonSoft.Json, we can use the deserialization method therein
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)
Output
6856
3667
This is superior to your method if you know the design of the data at design-time, because you get strong typing. Compare
Dim FileID = read.Item("files")
to
Dim fileIDs = rootobject.files.First().id
and see that in my example files
is a concrete property of the object, while in yours files
is a variable string you use to index a property which returns a JToken
Anyways I think you mainly had an issue with expecting a single value when an array was returned, which would be an issue in either case but with serialization you are notified by the compiler instead of at run-time.