У меня есть следующий код: он подключается к серверу PHP и извлекает данные успешно, я не очень хорошо разбираюсь в VB, как я могу прочитать текст ответа JSON и извлечь его?
Public Class Form1
Private Sub submit_Click(sender As System.Object, e As System.EventArgs) Handles submit.Click
Dim user As String
Dim pass As String
user = uname.Text
pass = passwd.Text
Dim request As WebRequest = WebRequest.Create("http://domain.com/test.php")
request.Method = "POST"
Dim postData As String
postData = "username=" & user & "&password=" & pass
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As WebResponse = request.GetResponse()
Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
dataStream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
If responseFromServer = "0" Then
MsgBox("Login Failed")
Else
MsgBox("json data")
End If
reader.Close()
dataStream.Close()
response.Close()
End Sub
End Class
Ответ JSON будет примерно таким:
{"comments": [
{
"comment" : "some text",
"date" : "some date",
"user" : "user name"
},
{
"comment" : "some text",
"date" : "some date",
"user" : "user name"
}
],
"messages": [ .... ]
}
Как вывести строку json в:
Comments
user date comment
-----------------------------------
user 1 date 1 comment 1
user 2 date 2 comment 2
Messages
user date message
-----------------------------------
user 1 date 1 message 1
user 2 date 2 message 2