|
-
Jul 18th, 2020, 09:42 AM
#4
Thread Starter
Hyperactive Member
Re: Get and read JSON data
got it working to some extend. changed from vs2010 to vs2017
below is the code i have done:
Code:
Dim json As String = "http://domainname/Clientapi/format_d_details"
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
request = DirectCast(WebRequest.Create("http://domainname/Clientapi/format_d_details"), HttpWebRequest)
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Dim rawresp As String
rawresp = reader.ReadToEnd()
Dim jResults As Object = JObject.Parse(rawresp)
Dim mFailed As String = If(jResults("failed") Is Nothing, "Record Found", jResults("failed").ToString)
If mFailed <> "0" Then
Dim ser As JObject = JObject.Parse(rawresp)
Dim data As List(Of JToken) = ser.Children().ToList
Dim output As String = ""
For Each item As JProperty In data
item.CreateReader()
Select Case item.Name
Case "failed"
Dim mMsg As String = "Failed"
Case "information"
For Each msg As JObject In item.Value
Dim drNewRow As DataRow = dTable.NewRow
drNewRow.Item("ICMR_no") = msg("ICMR_no")
drNewRow.Item("name") = msg("name")
drNewRow.Item("contact_number") = msg("contact_number")
drNewRow.Item("gender") = msg("gender")
drNewRow.Item("address") = msg("address")
drNewRow.Item("age") = msg("age")
drNewRow.Item("health_center") = msg("health_center")
drNewRow.Item("prabhag_name") = msg("prabhag_name")
drNewRow.Item("camp") = msg("camp")
drNewRow.Item("covid_detect_date") = msg("covid_detect_date")
drNewRow.Item("status") = msg("status")
drNewRow.Item("record_date") = msg("record_date")
drNewRow.Item("GatheredOn") = Format(Today.Date, "dd-MMM-yyyy")
dTable.Rows.Add(drNewRow)
'Dim first_name As String = msg("first_name")
'Dim last_name As String = msg("last_name")
Next
End Select
Next
End If
my prob now is i can read the data if it contains {"failed":0}
but the prob. is that the person sending me the data is putting the record no. too in the data. the data that is coming is as below:
{"information": {"1":{"ICMR_no":"22","name":"abc","contact_number":"12345","gender":"Female","address":"thane1","age":"2" ,"health_center":"Health Post - 2","prabhag_name":"Prabhag - 2","camp":"Camp 1","covid_detect_date":"2020-07-16","patient_status":"","record_date":"2020-07-18 12:17:13"}, "2":{"ICMR_no":"1001","name":"testing info","contact_number":"8959596595","gender":"Male","address":"ulhasnagar","age":"28","health_center ":"Health Post - 3","prabhag_name":"Prabhag - 3","camp":"Camp 2","covid_detect_date":"2020-07-01","patient_status":"Positive","record_date":"2020-07-18 18:10:42"}}}
the record no. is giving me prob.
Code:
For Each msg As JObject In item.Value
is showing me error
error is :
System.InvalidCastException: 'Unable to cast object of type 'Newtonsoft.Json.Linq.JProperty' to type 'Newtonsoft.Json.Linq.JObject'.'
confused a lot
pls. guide.
thanks
The only time you run out of chances is when you stop taking them.
The mind is like a parachute.
It doesn’t work unless it’s open.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|