代码之家  ›  专栏  ›  技术社区  ›  B.Spangenberg

SharePoint Online-分析动态JSON数组值

  •  0
  • B.Spangenberg  · 技术社区  · 8 年前

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

    ms-graph json response for sharepoint list fields:

    “fields”:。{
    “@odata.etag”:“\”54e4e975-d364-4064-a2f0-63172bd74836,13\”,
    “fileleafref”:“filenameboys.jpg”,
    “title”:“树”,
    “column_x0020_for_x0020_text_x0020_testing”:“awdada”,
    “链接”:。{
    “description”:“https://stackoverflow.com/questions/ask”,
    “url”:“https://stackoverflow.com/questions/ask”
    }
    “是”或“否”:正确,
    “id”:“1”,
    “contenttype”:“文档”,
    “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”:”,
    “_complianceStagWriteTime”:”“,
    “_complianceStaguserid”:”,
    “_commentCount”:”,
    ““likeCount”:”,
    “edit”:“0”,
    “uiversionString”:“12.0”,
    “parentVersionStringLookupID”:“1”,
    “parentLeafNameLookupID”:“1”
    }
    

    您将在上面的代码片段中看到,名为link的字段是json,但其他字段值的结果只是普通字符串。因此,我目前使用IDictionary<string,object>来分析数据,因为我不确定字段值将保留什么。

    现在,当我将IDictionary字段集合绑定到数据网格时,会得到以下结果。(如预期,causelinkhas not been properly parsed.只是被抛出到类型对象中。

    q:i'm not too sure how to go about analysing the values to enable me to bind directly to the hyperlink url.在解析和将其存储为对象之前,我是否应该进行某种类型的测试,以检查它是否是超链接(或任何其他类型的预期结果类型)。

    只是为了添加到上面,超链接列的JSON结果列没有指示它是什么类型。添加下面的列json作为参考。

    ms-graph json response for sharepoint list columnlink:

    。{
    “columngroup”:“自定义列”,
    “description”:“”,
    “displayname”:“链接”,
    “EnforceUniqueValues”:假,
    “隐藏”:错误,
    “id”:“97A58EDB-1F5C-4E46-B843-FC9827847088”,
    “indexed”:错误,
    “name”:“链接”,
    “readonly”:错误,
    “必选”:错误
    }
    
    
    

    用于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"
            }
    

    您将在上面的代码片段中看到名为链接是json,但其他字段值的结果只是普通字符串。所以我现在用IDictionary<string, object>为了解析数据,因为我不确定字段值将包含什么。

    现在,当我将IDictionary字段集合绑定到数据网格时,会得到以下结果。(如预期,原因链接未正确分析。刚刚被抛出到类型对象)

    Grid Result for IDictionary<string, object> binding

    问:我不太确定如何解析这些值以使我能够直接绑定到超链接URL。我应该做点什么样的排版检查它是否是超链接吗(或任何其他类型的预期结果类型)在解析并将其存储为对象之前。

    只是为了添加到上面,超链接列的JSON结果列没有指示它是什么类型。添加下面的列json作为参考。

    用于SharePoint列表列的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
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   OfirD    8 年前

    正如您已经发现的,您必须以某种方式解析嵌套的对象。有几种方法可以做到这一点。这是一个( 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));
                }
            }
        }
    }
    

    输出:

    Key: FileLeafRef, Value: FILENAMEBOYS.jpg 
    Key: link,
        Key: Description, Value: https://stackoverflow.com/questions/ask
        Key: Url, Value: https://stackoverflow.com/questions/ask 
    推荐文章