使用 JavaScriptSerializer 从 JSON 填充文本框
Filling a TextBox from JSON using JavaScriptSerializer
我正在尝试使用 JavaScriptSerializer
从网站 API 中提取一些信息。但是,我是 vb.net 中的新手,似乎无法加载文本框。我认为这与我从网站收到的 JSON 文本有关,它在顶部有一个状态,但我不知道如何解决这个问题。
这里是 JSON 文本:
{
"status": "ok",
"meta": {
"count": 1
},
"data": {
"1000003149": {
"members": [
{
"account_name": "CHOP210"
},
{
"account_name": "Guns_n_Roses"
},
{
"account_name": "vonhames"
},
{
"account_name": "wiggum99"
},
{
"account_name": "VonChalon"
},
{
"account_name": "FokkerDR1"
},
{
"account_name": "Force12"
},
{
"account_name": "m4irish"
},
{
"account_name": "panzer1_14"
},
{
"account_name": "alchemicalgunner007"
},
{
"account_name": "Morrros"
},
{
"account_name": "jonah128"
},
{
"account_name": "Zy4"
},
{
"account_name": "Terminator6644"
},
{
"account_name": "Dionisiovega"
}
]
}
}
}
这是我的代码:
Imports System.Net
Imports System.IO
Imports System.Web.Script.Serialization
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim uriString As String = "https://api.worldoftanks.com/wot/clans/info/?application_id=c2b5cb8d6c77098c8a9a481e68476b28&clan_id=1000003149&fields=members.account_name"
Dim uri As New Uri(uriString)
Dim Request As HttpWebRequest = HttpWebRequest.Create(uri)
Request.Method = "GET"
Dim Response As HttpWebResponse = Request.GetResponse()
Dim Read = New StreamReader(Response.GetResponseStream())
Dim Raw As String = Read.ReadToEnd()
Dim dict As Object = New JavaScriptSerializer().Deserialize(Of List(Of Object))(Raw)
For Each item As Object In dict
TextBox1.Text += item("account_name").ToString + vbNewLine
Next
End Sub
End Class
我认为文本框无法加载的原因是 JSON 文本的状态和数据部分。现在我只是填充一个多行文本框,但稍后我需要使用帐户 ID 将它们绑定到组合框。
问题是您试图将 JSON 反序列化为 List(Of Object)
但 JSON 实际上不是列表;相反,它是一系列嵌套对象,最里面的对象包含成员列表。 (我已经重新格式化了您问题中的 JSON 以使事情的结构更加明显。)您需要创建 类 来对 JSON 建模,然后反序列化到该模型中。
JSON 中的一个奇怪之处是 1000003149
键,我认为它是动态的(看起来这对应于 URL 中的氏族 ID)。所以你需要一本字典来处理那部分。
这里是 类 对应 JSON:
Public Class RootObject
Public Property status As String
Public Property meta As Meta
Public Property data As Dictionary(Of String, Clan)
End Class
Public Class Meta
Public Property count As Integer
End Class
Public Class Clan
Public Property members As List(Of Member)
End Class
Public Class Member
Public Property account_name As String
End Class
然后,您可以像这样反序列化并填充您的 TextBox:
Dim jss As New JavaScriptSerializer
Dim root As RootObject = jss.Deserialize(Of RootObject)(Raw)
For Each member As Member In root.data.First().Value.members
TextBox1.Text += member.account_name + vbNewLine
Next
我正在尝试使用 JavaScriptSerializer
从网站 API 中提取一些信息。但是,我是 vb.net 中的新手,似乎无法加载文本框。我认为这与我从网站收到的 JSON 文本有关,它在顶部有一个状态,但我不知道如何解决这个问题。
这里是 JSON 文本:
{
"status": "ok",
"meta": {
"count": 1
},
"data": {
"1000003149": {
"members": [
{
"account_name": "CHOP210"
},
{
"account_name": "Guns_n_Roses"
},
{
"account_name": "vonhames"
},
{
"account_name": "wiggum99"
},
{
"account_name": "VonChalon"
},
{
"account_name": "FokkerDR1"
},
{
"account_name": "Force12"
},
{
"account_name": "m4irish"
},
{
"account_name": "panzer1_14"
},
{
"account_name": "alchemicalgunner007"
},
{
"account_name": "Morrros"
},
{
"account_name": "jonah128"
},
{
"account_name": "Zy4"
},
{
"account_name": "Terminator6644"
},
{
"account_name": "Dionisiovega"
}
]
}
}
}
这是我的代码:
Imports System.Net
Imports System.IO
Imports System.Web.Script.Serialization
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim uriString As String = "https://api.worldoftanks.com/wot/clans/info/?application_id=c2b5cb8d6c77098c8a9a481e68476b28&clan_id=1000003149&fields=members.account_name"
Dim uri As New Uri(uriString)
Dim Request As HttpWebRequest = HttpWebRequest.Create(uri)
Request.Method = "GET"
Dim Response As HttpWebResponse = Request.GetResponse()
Dim Read = New StreamReader(Response.GetResponseStream())
Dim Raw As String = Read.ReadToEnd()
Dim dict As Object = New JavaScriptSerializer().Deserialize(Of List(Of Object))(Raw)
For Each item As Object In dict
TextBox1.Text += item("account_name").ToString + vbNewLine
Next
End Sub
End Class
我认为文本框无法加载的原因是 JSON 文本的状态和数据部分。现在我只是填充一个多行文本框,但稍后我需要使用帐户 ID 将它们绑定到组合框。
问题是您试图将 JSON 反序列化为 List(Of Object)
但 JSON 实际上不是列表;相反,它是一系列嵌套对象,最里面的对象包含成员列表。 (我已经重新格式化了您问题中的 JSON 以使事情的结构更加明显。)您需要创建 类 来对 JSON 建模,然后反序列化到该模型中。
JSON 中的一个奇怪之处是 1000003149
键,我认为它是动态的(看起来这对应于 URL 中的氏族 ID)。所以你需要一本字典来处理那部分。
这里是 类 对应 JSON:
Public Class RootObject
Public Property status As String
Public Property meta As Meta
Public Property data As Dictionary(Of String, Clan)
End Class
Public Class Meta
Public Property count As Integer
End Class
Public Class Clan
Public Property members As List(Of Member)
End Class
Public Class Member
Public Property account_name As String
End Class
然后,您可以像这样反序列化并填充您的 TextBox:
Dim jss As New JavaScriptSerializer
Dim root As RootObject = jss.Deserialize(Of RootObject)(Raw)
For Each member As Member In root.data.First().Value.members
TextBox1.Text += member.account_name + vbNewLine
Next