基本上,我正在尝试使用4chan JSON API解析来自4chan线程的注释。 https://github.com/4chan/4chan-API
基本上,有一个名为input的富文本框,另一个名为post_text_box。我想要做的是使它在输入文本框中输入4chan线程中的JSON,并从该JSON中提取注释并显示在输出文本框中
然而,每当我尝试点击Go按钮时都没有任何反应。
到目前为止,这是我的代码
Imports System.Web.Script.Serialization
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class Form1
Private Sub start_button_Click(sender As Object, e As EventArgs) Handles start_button.Click
Dim j As Object = New JavaScriptSerializer().Deserialize(Of Post)(input.Text)
post_text_box.Text = j.com
End Sub
End Class
Public Class Rootobject
Public Property posts() As Post
End Class
Public Class Post
Public Property no As Integer
Public Property now As String
Public Property name As String
Public Property com As String
Public Property filename As String
Public Property ext As String
Public Property w As Integer
Public Property h As Integer
Public Property tn_w As Integer
Public Property tn_h As Integer
Public Property tim As Long
Public Property time As Integer
Public Property md5 As String
Public Property fsize As Integer
Public Property resto As Integer
Public Property bumplimit As Integer
Public Property imagelimit As Integer
Public Property replies As Integer
Public Property images As Integer
End Class
答案 0 :(得分:17)
由于您要导入Newtonsoft.Json
,因此您只需使用JsonConvert.DeserializeObject<T>(String)
method:
Dim exampleJson As String = "{ 'no':'123', 'name':'Some Name', 'com':'This is a comment'}"
Dim post As Post = JsonConvert.DeserializeObject(Of Post)(exampleJson)
Dim com As String = post.com
post_text_box.Text = com
或者,如果您不想为Post
创建课程,则可以使用JsonConvert.DeserializeAnonymousType<T>(String, T)
:
Dim exampleJson As String = "{ 'no':'123', 'name':'Some Name', 'com':'This is a comment'}"
Dim tempPost = New With {Key .com = ""}
Dim post = JsonConvert.DeserializeAnonymousType(exampleJson, tempPost)
Dim com As String = post.com
post_text_box.Text = com
编辑:看起来您正在从API获取数组:
{
"posts" : [{
"no" : 38161812,
"now" : "11\/19\/13(Tue)15:18",
"name" : "Anonymous",
"com" : "testing thread for JSON stuff",
"filename" : "a4c",
"ext" : ".png",
"w" : 386,
"h" : 378,
"tn_w" : 250,
"tn_h" : 244,
"tim" : 1384892303386,
"time" : 1384892303,
"md5" : "tig\/aNmBqB+zOZY5upx1Fw==",
"fsize" : 6234,
"resto" : 0,
"bumplimit" : 0,
"imagelimit" : 0,
"replies" : 0,
"images" : 0
}
]
}
在这种情况下,您需要将要反序列化的类型更改为Post()
:
首先,添加另一个小包装类:
Public Class PostWrapper
Public posts() As Post
End Class
然后调整反序列化代码:
Dim json As String = input_box.Text
Dim postWrapper = JsonConvert.DeserializeObject(Of PostWrapper)(json) ' Deserialize array of Post objects
Dim posts = postWrapper.posts
If posts.Length = 1 Then ' or whatever condition you prefer
post_text_box.Text = posts(0).com
End If
答案 1 :(得分:8)
您可以将JSON反序列化为Object
,而不是需要定义类,如下所示:
Dim json As String = "{""items"":[{""Name"":""John"",""Age"":""20"",""Gender"":""Male""},{""Name"":""Tom"",""Age"":""25"",""Gender"":""Male""},{""Name"":""Sally"",""Age"":""30"",""Gender"":""Female""}]}"
Dim jss = New JavaScriptSerializer()
Dim data = jss.Deserialize(Of Object)(json)
现在,作为示例,您可以遍历反序列化的JSON并构建HTML表,如下所示:
Dim sb As New StringBuilder()
sb.Append("<table>" & vbLf & "<thead>" & vbLf & "<tr>" & vbLf)
' Build the header based on the keys of the first data item.
For Each key As String In data("items")(0).Keys
sb.AppendFormat("<th>{0}</th>" & vbLf, key)
Next
sb.Append("</tr>" & vbLf & "</thead>" & vbLf & "<tbody>" & vbLf)
For Each item As Dictionary(Of String, Object) In data("items")
sb.Append("<tr>" & vbLf)
For Each val As String In item.Values
sb.AppendFormat(" <td>{0}</td>" & vbLf, val)
Next
Next
sb.Append("</tr>" & vbLf & "</tbody>" & vbLf & "</table>")
Dim myTable As String = sb.ToString()
免责声明:我每天都使用C#,这是使用转换为VB.NET的dynamic
的C#示例,如果有任何语法错误,请原谅我。
答案 2 :(得分:0)
另外,如果你有复杂的json字符串。如果json字符串中有子类,数组等,您可以在下面使用这种方式。我试过了,它对我有用。我希望它对你有用。
我在json字符串中访问了root-&gt; simpleforecast-&gt; forecastday [] - &gt; date-&gt; hight-&gt; celsius,fahrenheit values等。
>>> myDict = {'foo': 'bar', 'foobar baz': 'qux'}
>>> {k:v for k, v in myDict.items() for k in k.split()}
{'baz': 'qux', 'foo': 'bar', 'foobar': 'qux'}
答案 3 :(得分:0)
注意:
首先,您必须在nuget控制台上安装Newtonsoft.Json。然后在代码的顶部包含以下代码。
def graph_clusters_new_mu(self, x, cluster_indice, ordinals):
new_mus = []
for i_cluster in range(self.n_clusters):
is_i_cluster = tf.squeeze(tf.equal(cluster_indice, i_cluster))
x_cluster = tf.boolean_mask(x, is_i_cluster)
x_cluster_mean = tf.reduce_mean(x_cluster, axis=0 )
new_mus.append( x_cluster_mean )
new_mu = tf.stack(new_mus)
return new_mu
步骤:1使用get&amp;创建课程设置属性。
Imports Newtonsoft.Json
步骤:2将字符串创建为json格式并转换为json对象模型。
Public Class Student
Public Property rno() As String
Get
Return m_rno
End Get
Set(value As String)
m_rno = value
End Set
End Property
Private m_rno As String
Public Property name() As String
Get
Return m_name
End Get
Set(value As String)
m_name = value
End Set
End Property
Private m_name As String
Public Property stdsec() As String
Get
Return m_StdSec
End Get
Set(value As String)
m_StdSec = value
End Set
End Property
Private m_stdsec As String
End Class
步骤:3只需遍历object.entity名称,如下所示。
Dim json As String = "{'rno':'09MCA08','name':'Kannadasan Karuppaiah','stdsec':'MCA'}"
Dim stuObj As Student = JsonConvert.DeserializeObject(Of Student)(json)