I want to be able to extract all the values from "cells" into three separate lists (one list for "columnIDs", another for "rowID" and another for "value"). But I am having difficulty in working with JObject in terms of extracting the data.
Here is an example of the JObject:
{
"id": 0,
"name": "SpreadSheet1",
"rows": [
{
"id": 0,
"cells": [
{
"columnId": 0,
"rowId": 0,
"value": "0393391D"
},
{
"columnId": 1,
"rowId": 0,
"value": "P0039233"
},
{
"columnId": 2,
"rowId": 0,
"value": "Milk"
},
{
"columnId": 3,
"rowId": 0,
"value": "p61-203"
}
]
},
{
"id": 1,
"cells": [
{
"columnId": 0,
"rowId": 1,
"value": "085485K"
},
{
"columnId": 1,
"rowId": 1,
"value": "P049596"
},
{
"columnId": 2,
"rowId": 1,
"value": "Coffee"
},
{
"columnId": 3,
"rowId": 1,
"value": "F49-T4J"
}
]
}
],
"columns": [
{
"id": 0,
"name": "Id"
},
{
"id": 1,
"name": "LocationID"
},
{
"id": 2,
"name": "ItemDescription"
},
{
"id": 3,
"name": "Destination"
}
]
}
CodePudding user response:
try this
var cells = JObject.Parse(json)["rows"].SelectMany(r => r["cells"]);
List<int> columnIds = cells.Select(c => (int)c["columnId"]).ToList();
List<int> rowIds = cells.Select(c => (int)c["rowId"]).ToList();
List<string> values = cells.Select(c => (string)c["value"]).ToList();