c# - How to iterate through JSON array from n1ql query? -
so i'm using couchbase queue enqueue beacon information. i'm trying use n1ql query method , i'm having trouble getting information. realized i'm getting first beacon entry because result.rows returns 1 element, array of beaconinfon1ql. wanted iterate through array , add each list.
try { var cluster = new cluster(new clientconfiguration()); using (var bucket = cluster.openbucket("beaconinfon1ql")) { string query = "select * `beaconinfon1ql`"; var queryrequest = new queryrequest(query); var result = bucket.query<dynamic>(queryrequest); foreach (var row in result.rows) { int = 0; var beacon = new beacon() { serialnumber = row.beaconinfon1ql[i].serialnumber, receiveddate = convert.todatetime(row.beaconinfon1ql[i].receiveddate), receiverid = row.beaconinfon1ql[i].receiverid, distance = convert.todouble(row.beaconinfon1ql[i].distance), rssi = convert.toint32(row.beaconinfon1ql[i].rssi), newdistance = convert.todouble(row.beaconinfon1ql[i].newdistance), distancetesting = convert.todouble(row.beaconinfon1ql[i].distancetesting), }; i++; _beaconslist.add(beacon); } } return _beaconslist;
my result.rows looks this
result.rows=
{{ "beaconinfon1ql": [ { "distance": 2.2705747109792007, "distancetesting": 22, "newdistance": 22, "receiveddate": "0001-01-01t00:00:00", "receiverid": "42008780c4b9b329", "rssi": -73, "serialnumber": "888" }, { "distance": 2.2705747109792007, "distancetesting": 22, "newdistance": 22, "receiveddate": "0001-01-01t00:00:00", "receiverid": "42008780c4b9b329", "rssi": -73, "serialnumber": "888" }, { "distance": 2.2705747109792007, "distancetesting": 22, "newdistance": 22, "receiveddate": "0001-01-01t00:00:00", "receiverid": "42008780c4b9b329", "rssi": -73, "serialnumber": "888" }, { "distance": 2.2705747109792007, "distancetesting": 22, "newdistance": 22, "receiveddate": "0001-01-01t00:00:00", "receiverid": "42008780c4b9b329", "rssi": -73, "serialnumber": "888" }, ] }}
i'm not sure how make second foreach/for loop iterate through keys.
for iterating json, use dynamics. here's example:
var result = new result() { rows = @"{ 'beaconinfon1ql': [ { 'distance': 2.2705747109792007, 'distancetesting': 22, 'newdistance': 22, 'receiveddate': '0001-01-01t00:00:00', 'receiverid': '42008780c4b9b329', 'rssi': -73, 'serialnumber': '888' } ] }" //other entries omitted brevity }; dynamic parsedrows = jsonconvert.deserializeobject(result.rows); foreach (var entry in parsedrows.beaconinfon1ql) debug.write(entry.distance);
note: got rid of double curly braces output in example.
Comments
Post a Comment