簡體   English   中英

如何從JObject獲取某些特定值

[英]How can I get some specific values from a JObject

我希望得到的文件,文件編號文件檔案大小 文件名,ID, 事故-reportedOn了以下JObject的:

注意兩個“ [[ ”在開頭。 我必須先減少JObject嗎?

[
[{
        "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"
                }]
            }
        }
    },

我假設你已經從兩個數組中提取了JObject 在這種情況下,您可以簡單地使用索引運算符來遍歷json文件,如下所示:

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

完整示例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM