C
Coderv9
Guest
Hi All,
I am trying to load a json file values to a text box.
I was success with reading and contents and but unable to use the results properly.
Below i what i was trying to achieve.
Imports System.Net
Imports System.IO
Imports System.Linq
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
'Try
request = DirectCast(WebRequest.Create("https://api.myjson.com/bins/jnoqm"), HttpWebRequest)
request.Proxy = proxysrv 'Proxy Credentials
request.Credentials = netCredential 'Webserver Credentials
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Dim rawresp As String
rawresp = reader.ReadToEnd()
'This code works. But The Json data is dynamic
'Dim res = JsonConvert.DeserializeObject(rawresp)
'MsgBox(res("result")(0)("number") & vbCrLf & res("result")(0)("opened_by") & vbCrLf & res("result")(0)("message") & vbCrLf & res("result")(0)("created"))
'MsgBox(res("result")(1)("number") & vbCrLf & res("result")(1)("opened_by") & vbCrLf & res("result")(1)("message") & vbCrLf & res("result")(1)("created"))
'MsgBox(res("result")(2)("number") & vbCrLf & res("result")(2)("opened_by") & vbCrLf & res("result")(2)("message") & vbCrLf & res("result")(2)("created"))
Dim allData As JObject = JObject.Parse(rawresp)
'Need to check if there are any tokens if tokens then do the follow
For Each token As JToken In allData("result")
'load contents to a text box
Next
End Sub
End Class
Here is the Json file:
{
"result": [
{
"number": "CHAT0010006",
"opened_by": "John",
"message": "Help !!!",
"created": "2020-01-30 11:45:34"
},
{
"number": "CHAT0010008",
"opened_by": "Victor",
"message": "Hello",
"created": "2020-01-31 08:58:47"
},
{
"number": "CHAT0010009",
"opened_by": "Peter",
"message": "Hi Team",
"created": "2020-01-31 09:03:21"
}
]
}
Coderv9
Continue reading...
I am trying to load a json file values to a text box.
I was success with reading and contents and but unable to use the results properly.
Below i what i was trying to achieve.
Imports System.Net
Imports System.IO
Imports System.Linq
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
'Try
request = DirectCast(WebRequest.Create("https://api.myjson.com/bins/jnoqm"), HttpWebRequest)
request.Proxy = proxysrv 'Proxy Credentials
request.Credentials = netCredential 'Webserver Credentials
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Dim rawresp As String
rawresp = reader.ReadToEnd()
'This code works. But The Json data is dynamic
'Dim res = JsonConvert.DeserializeObject(rawresp)
'MsgBox(res("result")(0)("number") & vbCrLf & res("result")(0)("opened_by") & vbCrLf & res("result")(0)("message") & vbCrLf & res("result")(0)("created"))
'MsgBox(res("result")(1)("number") & vbCrLf & res("result")(1)("opened_by") & vbCrLf & res("result")(1)("message") & vbCrLf & res("result")(1)("created"))
'MsgBox(res("result")(2)("number") & vbCrLf & res("result")(2)("opened_by") & vbCrLf & res("result")(2)("message") & vbCrLf & res("result")(2)("created"))
Dim allData As JObject = JObject.Parse(rawresp)
'Need to check if there are any tokens if tokens then do the follow
For Each token As JToken In allData("result")
'load contents to a text box
Next
End Sub
End Class
Here is the Json file:
{
"result": [
{
"number": "CHAT0010006",
"opened_by": "John",
"message": "Help !!!",
"created": "2020-01-30 11:45:34"
},
{
"number": "CHAT0010008",
"opened_by": "Victor",
"message": "Hello",
"created": "2020-01-31 08:58:47"
},
{
"number": "CHAT0010009",
"opened_by": "Peter",
"message": "Hi Team",
"created": "2020-01-31 09:03:21"
}
]
}
Coderv9
Continue reading...