简体   繁体   中英

How can I get some specific values from a JObject

I want to get the FILE-file-id , FILE-fileSize FILENAME-id , INCIDENT-reportedOn out of the following JObject:

Note the two " [[ " at the beginning. Do I have to reduce the JObject first?

[
[{
        "FILENAME": {
            "id": "renamedtopdf.docx.pdf",
            "label": "fileName",
            "type": "vertex"
        },
        "FILE": {
            "id": "dc92d48b7e29c528b3eb168446e51736101122a821c9e712320bd6842116719a",
            "label": "file",
            "type": "vertex",
            "properties": {
                "fileSize": [{
                    "id": "f9339436-189a-4503-abc6-e2989be6f138",
                    "value": "164198"
                }],
                "mimeType": [{
                    "id": "0a89dbfa-c204-45c8-8524-3fbd02b04e39",
                    "value": "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"
                }]
            }
        },
        "INCIDENT": {
            "id": "16ea8c8b-65ee-44b3-afbb-98308b092b4f",
            "label": "incident",
            "type": "vertex",
            "properties": {
                "reportedOn": [{
                    "id": "81485296-a62f-4d17-a03f-4995c3cad937",
                    "value": "2/16/2019 10:33:59 AM"
                }]
            }
        }
    },

I'll assume you already extracted the JObject from the two arrays. I that case you can simply use the index operator to traverse the json file like so:

json["FILE"]["id"].Value<string>();
json["FILE"]["properties"]["fileSize"]["value"].Value<string>();
json["FILENAME"]["id"].Value<string>();
json["INCIDENT"]["properties"]["reportedOn"]["Value"].Value<string>();

Full Example:

const string text = @"{
    "FILENAME": {
        "id": "renamedtopdf.docx.pdf",
        "label": "fileName",
        "type": "vertex"
    },
    "FILE": {
        "id": "dc92d48b7e29c528b3eb168446e51736101122a821c9e712320bd6842116719a",
        "label": "file",
        "type": "vertex",
        "properties": {
            "fileSize": [
                {
                    "id": "f9339436-189a-4503-abc6-e2989be6f138",
                    "value": "164198"
                }
            ],
            "mimeType": [
                {
                    "id": "0a89dbfa-c204-45c8-8524-3fbd02b04e39",
                    "value": "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"
                }
            ]
        }
    },
    "INCIDENT": {
        "id": "16ea8c8b-65ee-44b3-afbb-98308b092b4f",
        "label": "incident",
        "type": "vertex",
        "properties": {
            "reportedOn": [
                {
                    "id": "81485296-a62f-4d17-a03f-4995c3cad937",
                    "value": "2/16/2019 10:33:59 AM"
                }
            ]
        }
    }
}";
var json = JObject.Parse(text);
json["FILE"]["id"].Value<string>();
json["FILE"]["properties"]["fileSize"]["value"].Value<string>();
json["FILENAME"]["id"].Value<string>();
json["INCIDENT"]["properties"]["reportedOn"]["Value"].Value<string>();

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