I’m building JSON data to pass it to Chart JavaScript. With one item it works well.
Dim Person as new JSONItem
Person.Value("x") = 10
Person.Value("y") = 14
Person.Value("value") = "PRINCE01"
Dim finalString As String
finalString = "[" + Person.Tostring() + "]"
MsgBox " Person.Tostring " + Person.Tostring()
htmlChart.ExecuteJavaScript("showChart(" + finalString + ");")
I need to get lots of database rows and put it into JSON items.
How can I make several items of JSON data pair like {x:10, y:14, value:PRINCE01},{x:30, y:54, value:OWNER} ?
I thought of array structure but I’m not sure how I can employ it in this code.
First of all, I suggest not using manual assembly by just concatenation strings. If you want to create an array, you’ll need to create a level of structure above the items you want to group.
[code]dim root as new JSONItem
Dim Person as new JSONItem
Person.Value(“x”) = 10
Person.Value(“y”) = 14
Person.Value(“value”) = “PRINCE01”
Root.append Person
Dim Person2 as new JSONItem
Person2.Value(“x”) = 10
Person2.Value(“y”) = 14
Person2.Value(“value”) = “PRINCE01”
If I just append the Person1/Person2/Person3, I think I can see the final string.
However, the rows of the database are not fixed so I can’t define the number of Person before.
I agree with your thought as for manual assembly.
I’m looking for the way I can generate the JSON string more automatically.
Dim root as new JSONItem
while Not EOF
For i = 0 To rs.listcount .........
Person.Value("x") = rs.IdxField(1).StringValue
Person.Value("y") = rs.IdxField(2).StringValue
Person.Value("value") = rs.IdxField(3).StringValue
Root.append Person
Wend
Dim finalString As String
finalString = "[" + root.toString + "]"
Dim Person as new JSONItem
Dim root as new JSONItem
Dim finalString As String
While Not rs.EOF
Person.Value("x") = rs.IdxField(1).StringValue
Person.Value("y") = rs.IdxField(2).StringValue
Person.Value("value") = rs.IdxField(3).StringValue
root.append Person
rs.MoveNext
Wend
finalString = root.toString
I found one problem in the code example above.
I thought the Person would be replaced with the new value, but it doesn’t seem to be.
Dim Person as new JSONItem
This code should be inside of While loop. Otherwise, the final result comprises all of the same values.