SharePoint Online - 解析动态 JSON 数组值

SharePoint Online - Parsing dynamic JSON array values

所以我正在使用 MS Graph 进行 SharePoint Online 集成。 我的问题是 MS Graph 显示超链接字段值的方式。我不太确定如何处理这些动态字段的解析。请参阅下面的代码片段。

SharePoint 列表字段的 MS Graph JSON 响应:

 "fields": {
            "@odata.etag": "\"54e4e975-d364-4064-a2f0-63172bd74836,13\"",
            "FileLeafRef": "FILENAMEBOYS.jpg",
            "Title": "Trees",
            "Column_x0020_For_x0020_Text_x0020_Testing": "awdada",
            "link": {
                "Description": "https://whosebug.com/questions/ask",
                "Url": "https://whosebug.com/questions/ask"
            },
            "YES_x0020_OR_x0020_NO": true,
            "id": "1",
            "ContentType": "Document",
            "Created": "2018-06-28T16:57:34Z",
            "AuthorLookupId": "6",
            "Modified": "2018-07-19T13:59:12Z",
            "EditorLookupId": "6",
            "_CheckinComment": "",
            "LinkFilenameNoMenu": "FILENAMEBOYS.jpg",
            "LinkFilename": "FILENAMEBOYS.jpg",
            "DocIcon": "jpg",
            "FileSizeDisplay": "1048862",
            "ItemChildCount": "0",
            "FolderChildCount": "0",
            "_ComplianceFlags": "",
            "_ComplianceTag": "",
            "_ComplianceTagWrittenTime": "",
            "_ComplianceTagUserId": "",
            "_CommentCount": "",
            "_LikeCount": "",
            "Edit": "0",
            "_UIVersionString": "12.0",
            "ParentVersionStringLookupId": "1",
            "ParentLeafNameLookupId": "1"
        }

你会看到在上面的代码片段中,名为Link的字段是JSON,但其他字段值的结果只是正常字符串。所以我目前使用 IDictionary<string, object> 来解析数据,因为我不确定字段值将包含什么。

现在,当我将我的 IDictionary 字段集合绑定到我的数据网格时,我得到了以下结果。 (不出所料,导致Link没有被正确解析,直接抛到类型Object。)

问: 我不太确定如何解析值以使我能够直接绑定到超链接 URL。在解析并将其存储为对象之前,我是否应该做某种 TryParse 来检查它是否是超链接( 或任何其他类型的预期结果类型)。

补充一下,超链接列的 JSON 列结果没有说明它是什么类型。添加了下面的列 JSON 作为参考。

SharePoint 列表列 LINK:

的 MS Graph JSON 响应
{
      "columnGroup": "Custom Columns",
      "description": "",
      "displayName": "link",
      "enforceUniqueValues": false,
      "hidden": false,
      "id": "97a58edb-1f5c-4e46-b843-fc9827847088",
      "indexed": false,
      "name": "link",
      "readOnly": false,
      "required": false
}

如您所想,您必须以某种方式解析嵌套对象。有几种方法可以做到这一点。这是一个 (link to fiddle):

using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq; 

public class Program
{
    public static void Main()
    {       
        string someJson =@"{
            ""FileLeafRef"": ""FILENAMEBOYS.jpg"",
            ""link"": {
                ""Description"": ""https://whosebug.com/questions/ask"",
                ""Url"": ""https://whosebug.com/questions/ask""
            }
        }";
        var jObject = JObject.Parse(someJson);
        var parentDict = jObject.ToObject<Dictionary<string, object>>();
        foreach (var parentPair in parentDict) 
        {
            Console.Write(string.Format("Key: {0}, ", parentPair.Key));
            if (parentPair.Value is JObject)
            {
                Console.WriteLine();
                var childDict = ((JObject)parentPair.Value).ToObject<Dictionary<string, string>>();
                foreach (var childPair in childDict) 
                {
                    Console.WriteLine(string.Format("\tKey: {0}, Value: {1}", childPair.Key, childPair.Value));
                }
            }
            else if (parentPair.Value is string)
            {
                Console.WriteLine(string.Format("Value: {0}", parentPair.Value));
            }
        }
    }
}

输出:

Key: FileLeafRef, Value: FILENAMEBOYS.jpg 
Key: link,
    Key: Description, Value: https://whosebug.com/questions/ask
    Key: Url, Value: https://whosebug.com/questions/ask