1. 程式人生 > 實用技巧 >C# 獲取Newtonsoft.Json的JObject多層節點內容

C# 獲取Newtonsoft.Json的JObject多層節點內容

json形如(企業微信稽核介面返回結果)

{
    "errcode": 0,
    "errmsg": "ok. Warning: wrong json format. ",
    "info": {
        "sp_no": "202011300016",
        "sp_name": "請假",
        "sp_status": 2,
        "template_id": "xxxxxxxxxxxxxxxxx",
        "apply_time": 1606703944,
        "applyer": {
            "userid": "
Z2019-018", "partyid": "10" }, "sp_record": [{ "sp_status": 2, "approverattr": 1, "details": [{ "approver": { "userid": "Z2019-509" }, "speech": "", "sp_status
": 2, "sptime": 1606704031, "media_id": [] }, { "approver": { "userid": "Z2019-043" }, "speech": "", "sp_status": 1, "sptime": 0, "media_id": [] }] }, {
"sp_status": 2, "approverattr": 1, "details": [{ "approver": { "userid": "Z2018-054" }, "speech": "", "sp_status": 2, "sptime": 1606704117, "media_id": [] }] }], "notifyer": [{ "userid": "Z2017-421" }, { "userid": "Z2019-018" }], "apply_data": { "contents": [{ "control": "Vacation", "id": "vacation-1563793073xxx", "title": [{ "text": "請假型別", "lang": "zh_CN" }, { "text": "Leave Type", "lang": "en" }], "value": { "tips": [], "members": [], "departments": [], "files": [], "children": [], "stat_field": [], "vacation": { "selector": { "type": "single", "options": [{ "key": "9", "value": [{ "text": "事假", "lang": "zh_CN" }] }] }, "attendance": { "date_range": { "type": "hour", "new_begin": 1606780800, "new_end": 1606899600, "new_duration": 57600 }, "type": 1, "slice_info": { "day_items": [{ "daytime": 1606752000, "time_sections": [], "duration": 28800 }, { "daytime": 1606838400, "time_sections": [], "duration": 28800 }], "duration": 57600 } } }, "sum_field": [], "related_approval": [], "students": [], "classes": [] } }, { "control": "Textarea", "id": "item-14975813xxx01", "title": [{ "text": "請假事由", "lang": "zh_CN" }, { "text": "Leave Reason", "lang": "en" }], "value": { "text": "家裡有事", "tips": [], "members": [], "departments": [], "files": [], "children": [], "stat_field": [], "sum_field": [], "related_approval": [], "students": [], "classes": [] } }, { "control": "Text", "id": "Text-15908100xxx", "title": [{ "text": "備註", "lang": "zh_CN" }], "value": { "text": "", "tips": [], "members": [], "departments": [], "files": [], "children": [], "stat_field": [], "sum_field": [], "related_approval": [], "students": [], "classes": [] } }, { "control": "File", "id": "item-1497581426xxx", "title": [{ "text": "說明附件", "lang": "zh_CN" }, { "text": "Attachment", "lang": "en" }], "value": { "tips": [], "members": [], "departments": [], "files": [], "children": [], "stat_field": [], "sum_field": [], "related_approval": [], "students": [], "classes": [] } }] }, "comments": [] } }

JObject json1 = (JObject)JsonConvert.DeserializeObject(data);//data上述返回結果

JArray array = (JArray)(json1["info"]["apply_data"]["contents"]);//陣列

foreach (var jObject in array)

{

  //取資料

}

參考於:https://www.cnblogs.com/cwr-toki/p/10092089.html