Hello,
I’m trying to load json data which i get through httpsocket for the first time, example is here:
[{“id”:“1”,“product_name”:“FeSi”},{“id”:“2”,“product_name”:“FeMn”},{“id”:“3”,“product_name”:“FeCr”},{“id”:“4”,“product_name”:“Cu”}]
Dim s As String = DefineEncoding(content, Encodings.UTF8)
Dim Response AS New JSONItem
Response.Load(s)
Dim id, product_name As JSONItem
Dim JSONName, JSONValue as String
Dim coun AS Integer = Response.Count-1
JSONName = Response.Value("id")
For i As Integer = 0 To Response.Count-1
JSONName = Response.Name(i)
JSONValue = Response.Value(JSONName)
Textfield1.text = JSONName + " " + JSONValue + Endofline
Next
End if
I get error An exception of class JSONException was not handled. The application must shut down.
What does the Message property of the exception say?
Actually… look at the line that says
jsonname = response.value("id")
Response is an array.
I have tested this on Raspberry PI, now on PC, I see that id is Nil.
I have tried with following code:
For i As Integer = 0 To Response.Count-1
JSONName = Response.Name(i)
id = Response.Value(JSONName)
Textfield1.text = JSONName + " " + JSONValue + Endofline
next
But it is still id = Nil
Response.Count-1 has value 3 which is correct
I would like to save that json to sqlite table.
Greg gave you the answer above. The top level of your JSON is an array, not an object, so Name is meaningless. Each item of the array is an object that has a Name, but your code never accesses those.
Try something like this:
For i As Integer = 0 To Response.Count-1
dim item as JSONItem = Response( i )
id = item.Value( "id" )
Textfield1.AppendText "id = " + id + Endofline
next
Thank You Kem, your code works OK.