【问题标题】:SharePoint Online - Parsing dynamic JSON array valuesSharePoint Online - 解析动态 JSON 数组值
【发布时间】:2018-12-28 12:40:29
【问题描述】:

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

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://stackoverflow.com/questions/ask",
                "Url": "https://stackoverflow.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"
        }

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

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

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

只是补充一下,超链接列的列 JSON 结果没有指示它是什么类型。添加以下列 JSON 作为参考。

SharePoint 列表列的 MS Graph JSON 响应LINK

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

【问题讨论】:

  • 我的回答是否帮助您解决了问题?如果是,请将其标记为已接受。
  • 啊,抱歉。我以为我这样做了。我现在这样做了。谢谢。

标签: c# json microsoft-graph-api sharepoint-online


【解决方案1】:

正如您已经想到的,您必须以某种方式解析嵌套对象。有几种方法可以做到这一点。这是一个(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://stackoverflow.com/questions/ask"",
                ""Url"": ""https://stackoverflow.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));
            }
        }
    }
}

输出:

键:FileLeafRef,值:FILENAMEBOYS.jpg
关键:链接,
    键:描述,值:https://stackoverflow.com/questions/ask
    键:网址,值:https://stackoverflow.com/questions/ask 

【讨论】:

    猜你喜欢
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多