JSONItem problem with recordset

I am trying to create a JSONItem with data from a recordset. The format looks rights, but data from the last record is repeated in all the records. My code is

[code] dim j As new JSONItem
dim cat As new JSONItem
dim meter as new JSONItem
j.Value(“datasource”) = “bos://buildingos-json/” + tfGateway.Text

dim rs As RecordSet = app.ConfigDB.SQLSelect(“SELECT Name, Hidden, UnitID FROM Units WHERE Hidden = 0 ORDER BY UnitID LIMIT 3”)
if rs<>nil and not rs.EOF Then
while not rs.EOF
meter.Value(“meterId”) = “EZMeter_” + Format(rs.Field(“UnitID”).IntegerValue, “0000”)
meter.Value(“meterName”) = rs.Field(“Name”).StringValue
meter.Value(“meterUnits”) = “KWH”
cat.Append meter
rs.MoveNext
Wend
j.Value(“meterCatalog”) = cat
Else
return False
end if

dim s As String = j.ToString
TextArea1.Text = s[/code]

It should come out something like this

[quote]{
“datasource”: “bos://buildingosjson/mysite”,
“meterCatalog”: [
{
“meterId”: “1a2b3c4d5e6f”,
“meterName”: “Input 1 totalizer”,
“meterUnits”: “kWh”,
},
{
“meterId”: “1f2e3d5c5b6a”,
“meterName”: “Input 2 totalizer”,
“meterUnits”: “kWh”,
},[/quote]
but the meterId and meterName fields are all from the last record. When I step through it cat has the first record data in it once, then I get the second record data twice and then finally the third record’s data is repeated three time. Obviously, I need each record’s data once. When I send it to TextArea1, it is formatted correctly; just the data is wrong.

you created a new meter OUTSIDE the scope of your data loop

An object is a POINTER… so you created only a single instance, and then changed the values for that ONE instance

move the line

dim meter as new JSONItem

AFTER

 while not rs.EOF

this will create a NEW Instance to be appended to CAT

You need to make meter a new item for every iteration of the loop.

yeah… what Dave said :slight_smile:

lol our posts were 30 seconds apart :stuck_out_tongue:

You need to create a new meter instance within the loop.

:stuck_out_tongue: