简体   繁体   中英

Working with JObject and extracting values

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"
        }
    ]
}

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM