[英]JSON Data into DatagridView in VB.NET
I currently try to get my JSON Data into a DatagridView so it is in a reader-friendly format.我目前尝试将我的 JSON 数据放入 DatagridView 中,以便它采用读者友好的格式。 I use this to get my JSON String:
我用它来获取我的 JSON 字符串:
Dim Uri As New Uri("LINK")
Dim jsonString As String = "{ ""Version"": ""1"" }"
Dim data = Encoding.UTF8.GetBytes(jsonString)
Dim result_post = SendRequest(Uri, data, "application/json", "POST")
MsgBox(result_post)
This is what I get on the MsgBox这是我在 MsgBox 上得到的
{
"code":200,
"status":"OK",
"data":{
"Catalog":[
{
"ItemId":"shield_level_5",
"ItemClass":"shields",
"CatalogVersion":"1",
"DisplayName":"Level 5 Shield",
"Description":"Strong defense against projectile attacks",
"VirtualCurrencyPrices":{
"RM":199,
"VC":25
},
"RealCurrencyPrices":{
"GBP":149,
"EUR":169
}
},
{
"ItemId":"energy_boost_1",
"ItemClass":"boosts",
"CatalogVersion":"1",
"DisplayName":"Energy Drink Six-Pack",
"Description":"Get a quick burst of energy whenever you need it",
"VirtualCurrencyPrices":{
"RM":199,
"VC":25
}
},
{
"ItemId":"required_item_for_grant_a_1",
"ItemClass":"sets",
"CatalogVersion":"1",
"DisplayName":"Grant Set Item 1",
"Description":"Item 1 from the set of items needed to get Granted Item A for free",
"VirtualCurrencyPrices":{
"RM":249,
"VC":1500
}
},
{
"ItemId":"required_item_for_grant_a_2",
"ItemClass":"sets",
"CatalogVersion":"1",
"DisplayName":"Grant Set Item 2",
"Description":"Item 2 from the set of items needed to get Granted Item A for free",
"VirtualCurrencyPrices":{
"RM":249,
"VC":1500
}
},
{
"ItemId":"required_item_for_grant_a_3",
"ItemClass":"sets",
"CatalogVersion":"1",
"DisplayName":"Grant Set Item 3",
"Description":"Item 3 from the set of items needed to get Granted Item A for free",
"VirtualCurrencyPrices":{
"RM":249,
"VC":1500
}
},
{
"ItemId":"required_item_for_grant_a_4",
"ItemClass":"sets",
"CatalogVersion":"1",
"DisplayName":"Grant Set Item 4",
"Description":"Item 4 from the set of items needed to get Granted Item A for free",
"VirtualCurrencyPrices":{
"RM":249,
"VC":1500
}
},
{
"ItemId":"granted_item_a",
"ItemClass":"sets",
"CatalogVersion":"1",
"DisplayName":"Granted Item A",
"Description":"Bonus item granted for free once the full set of 4 Grant Set items are collected",
"GrantedIfPlayerHas":[
"required_item_for_grant_a_1",
"required_item_for_grant_a_2",
"required_item_for_grant_a_3",
"required_item_for_grant_a_4"
]
},
{
"ItemId":"consumable_item_1",
"ItemClass":"Consumables",
"CatalogVersion":"1",
"DisplayName":"Consumable Item 1",
"Description":"Item demonstrating usage-based consumables",
"VirtualCurrencyPrices":{
"VC":500
},
"Consumable":{
"UsageCount":3
}
},
{
"ItemId":"consumable_item_2",
"ItemClass":"Consumables",
"CatalogVersion":"1",
"DisplayName":"Consumable Item 2",
"Description":"Item demonstrating time-based consumables",
"VirtualCurrencyPrices":{
"VC":750
},
"Consumable":{
"UsagePeriod":3600,
"UsagePeriodGroup":"usage_group_1"
}
},
{
"ItemId":"sub_bundle_item_1",
"ItemClass":"Bundles",
"CatalogVersion":"1",
"DisplayName":"Bundled Item 1",
"Description":"Item which is added to the user inventory as part of a bundle"
},
{
"ItemId":"sub_bundle_item_2",
"ItemClass":"Bundles",
"CatalogVersion":"1",
"DisplayName":"Bundled Item 2",
"Description":"Item which is added to the user inventory as part of a bundle"
},
{
"ItemId":"item_bundle_1",
"ItemClass":"Bundles",
"CatalogVersion":"1",
"DisplayName":"Item bundle 1",
"Description":"Item which, when added to the user inventory, also adds other bundled items",
"VirtualCurrencyPrices":{
"RM":599
},
"Bundle":{
"BundledItems":[
"sub_bundle_item_1",
"sub_bundle_item_2"
],
"BundledResultTables":[
"random_result_1"
],
"BundledVirtualCurrencies":{
"VC":1000
}
}
},
{
"ItemId":"key_item_1",
"ItemClass":"Containers",
"CatalogVersion":"1",
"DisplayName":"Key Item 1",
"Description":"Key used to unlock Container 1",
"VirtualCurrencyPrices":{
"RM":199
}
},
{
"ItemId":"sub_container_item_1",
"ItemClass":"Containers",
"CatalogVersion":"1",
"DisplayName":"Container Item 1",
"Description":"Item which is added to the user inventory as part of opening a container"
},
{
"ItemId":"sub_container_item_2",
"ItemClass":"Containers",
"CatalogVersion":"1",
"DisplayName":"Container Item 2",
"Description":"Item which is added to the user inventory as part of opening a container"
},
{
"ItemId":"item_container_1",
"ItemClass":"Containers",
"CatalogVersion":"1",
"DisplayName":"Item Container 1",
"Description":"Item which, when opened with Key Item 1, also adds the items it holds to the user inventory",
"VirtualCurrencyPrices":{
"RM":499
},
"Consumable":{
"UsageCount":1
},
"Container":{
"KeyItemId":"key_item_1",
"ItemContents":[
"sub_container_item_1",
"sub_container_item_2"
],
"ResultTableContents":[
"random_result_1"
],
"VirtualCurrencyContents":{
"VC":1000
}
}
},
{
"ItemId":"item_container_2",
"ItemClass":"Containers",
"CatalogVersion":"1",
"DisplayName":"Item Container 2",
"Description":"Item which, when opened (no key needed), adds the items it holds to the user inventory",
"VirtualCurrencyPrices":{
"RM":99
},
"Consumable":{
"UsageCount":1
},
"Container":{
"VirtualCurrencyContents":{
"VC":1000
}
}
},
{
"ItemId":"starterpack_1",
"ItemClass":"starterpacks",
"CatalogVersion":"1",
"DisplayName":"Super Starter Pack",
"Description":"Everything you need to get started fast track at the right price"
}
]
}
}
Now, I want to get the data of each item in the JSON String into a DatagridView which looks like this:现在,我想将 JSON 字符串中每个项目的数据放入一个 DatagridView 中,如下所示:
I tried using我尝试使用
Dim obj = JsonConvert.DeserializeObject(Of JSON_result)(result_post)
but I don't know how to continue further and loop each item in the string.但我不知道如何继续并循环字符串中的每个项目。 When I try to get items out directly the string it is empty.
当我尝试直接从字符串中取出项目时,它是空的。
I hope someone can help me.我希望有一个人可以帮助我。
Greetz, Kia.格雷茨,起亚。
If you know the answer is always with the same format you can use the library如果您知道答案始终使用相同的格式,则可以使用该库
Newtonsoft.Json.Linq to trabsform the string into a JSONObject and the intern JSONArray. Newtonsoft.Json.Linq将字符串转换为 JSONObject 和实习生 JSONArray。 Then you will have a JSONObject with 2 "Strings" (code and status) and a JSONArray (data).
然后您将拥有一个带有 2 个“字符串”(代码和状态)和一个 JSONArray(数据)的 JSONObject。
Then with this you can create a class with the Public Properties: Id, Name, Description, VC_Price and RM_Price.然后,您可以使用公共属性创建一个类:Id、Name、Description、VC_Price 和 RM_Price。 Where you put the info from the JSONArray into those, and those into a BindingList(Of thatClass)
您将 JSONArray 中的信息放入这些信息中,并将这些信息放入BindingList(Of thatClass)
And then you simply pass the BlindingList to the DataGridView DataSource like this: table1.DataSource = bindingList1然后你只需像这样将 BlindingList 传递给 DataGridView 数据源: table1.DataSource = bindingList1
have you understood me?你了解我吗?
try this you can use current API试试这个你可以使用当前的API
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim api As String = "https://jsonplaceholder.typicode.com/users/"
Try
request = DirectCast(WebRequest.Create(api), HttpWebRequest)
Dim WebClient = New WebClient
response = DirectCast(request.GetResponse(), HttpWebResponse)
Dim result As String = WebClient.DownloadString(api)
'MsgBox(result)
Dim Results() = JsonConvert.DeserializeObject(Of GetTest())(result)
tb = New DataTable
For Each items As GetTest In Results
'dataholder += String.Format("{0}:{1} ,", items.Name)
'tb.Rows(0)(0) = items.Name
DataGridView1.Rows.Add(items.Name)
'MsgBox()
Next
'dataholder = dataholder.Remove(dataholder.Length - 2)
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.