在UWP中将JSON反序列化后,计算出的Sharepoint值返回Null

时间:2018-08-26 12:11:06

标签: c# json uwp get sharepoint-online

所以我试图从一个共享点列表中返回几个ListItems。我得到的内容还不错,但是当它反序列化内容时,计算所得的列将返回空值。我只是不明白,因为内容中有价值。

string geturl = "https://graph.microsoft.com/v1.0/sites/XXXXX.sharepoint.com,495435b4-60c3-49b7-8f6e-1d262a120ae5,0fad9f67-35a8-4c0b-892e-113084058c0a/lists/18a725ac-83ef-48fb-a5cb-950ca2378fd0/items/9?expand=fields(select=SerialNumber,id)";        

public async Task<string> GetDataWithTokenAsync(string url, string token, string listitem)
        {
            var httpClient = new System.Net.Http.HttpClient();
            System.Net.Http.HttpResponseMessage response;
            try
            {
                var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url);
                //Add the token in Authorization header
                request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
                response = await httpClient.SendAsync(request);
                var content = await response.Content.ReadAsStringAsync();
                var result = JsonConvert.DeserializeObject<SharePointListItems.RootObject>(content);
                if (listitem == "id")
                {
                    return result.fields.Id;
                }
                else
                {
                    return result.fields.SerialNumber;
                }
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }

这是课程(已缩短):

public class SharePointListItems
{
    public class Fields
    {
        [JsonProperty("@odata.etag")]
        public string ODataETag { get; set; }

        //General Parameters
        ...
        public string SerialNumber { get; set; } // Calculated field in Sharepoint
        public string Id { get; set; }

        ...
    }

    public class RootObject
    {
        [JsonProperty("@odata.context")]
        public string ODataContext { get; set; }

        [JsonProperty("@odata.etag")]
        public string ODataETag { get; set; }

        [JsonProperty("fields@odata.context")]
        public string FieldsODataContext { get; set; }

        public Fields fields { get; set; }
    }
}

我的GET返回的内容是:

"{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#sites('XXXXX.sharepoint.com%2C495435b4-60c3-49b7-8f6e-1d262a120ae5%2C0fad9f67-35a8-4c0b-892e-113084058c0a')/lists('18a725ac-83ef-48fb-a5cb-950ca2378fd0')/items/$entity\",\"@odata.etag\":\"\\\"a69b1840-239d-42ed-9b20-8789761fb06a,3\\\"\",\"createdDateTime\":\"2018-08-25T22:44:16Z\",\"eTag\":\"\\\"a69b1840-239d-42ed-9b20-8789761fb06a,3\\\"\",\"id\":\"9\",\"lastModifiedDateTime\":\"2018-08-25T22:44:16Z\",\"webUrl\":\"https://XXXXX.sharepoint.com/sites/GeneratorApp/Lists/GenApp/9_.000\",\"createdBy\":{\"user\":{\"email\":\"XXXXX@XXXXX.com\",\"id\":\"b0465821-e891-4f44-9e18-27e875f1b75d\",\"displayName\":\"XXXXX\"}},\"lastModifiedBy\":{\"user\":{\"email\":\"XXXXX@XXXXX.com\",\"id\":\"b0465821-e891-4f44-9e18-27e875f1b75d\",\"displayName\":\"XXXXX\"}},\"parentReference\":{},\"contentType\":{\"id\":\"0x0100E19591A4ECA81542AEA41A6AAFED6781\"},\"fields@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#sites('XXXXX.sharepoint.com%2C495435b4-60c3-49b7-8f6e-1d262a120ae5%2C0fad9f67-35a8-4c0b-892e-113084058c0a')/lists('18a725ac-83ef-48fb-a5cb-950ca2378fd0')/items('9')/fields/$entity\",\"fields\":{\"@odata.etag\":\"\\\"a69b1840-239d-42ed-9b20-8789761fb06a,3\\\"\",\"SerialNumber\":\"20180824-1353-DC6-Generator-A\",\"id\":\"9\"}}"

如果我添加一行

Jobject json = Jobject.Parse(content);

我收到以下响应(更改端点以获取多个ListItem):

{{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('XXXXX.sharepoint.com%2C495435b4-60c3-49b7-8f6e-1d262a120ae5%2C0fad9f67-35a8-4c0b-892e-113084058c0a')/lists('18a725ac-83ef-48fb-a5cb-950ca2378fd0')/items",
  "value": [
    {
      "@odata.etag": "\"a69b1840-239d-42ed-9b20-8789761fb06a,3\"",
      "createdDateTime": "2018-08-25T22:44:16Z",
      "eTag": "\"a69b1840-239d-42ed-9b20-8789761fb06a,3\"",
      "id": "9",
      "lastModifiedDateTime": "2018-08-25T22:44:16Z",
      "webUrl": "https://XXXXX.sharepoint.com/sites/GeneratorApp/Lists/GenApp/9_.000",
      "createdBy": {
        "user": {
          "email": "XXXXX@XXXXX.com",
          "id": "b0465821-e891-4f44-9e18-27e875f1b75d",
          "displayName": "XXXXX"
        }
      },
      "lastModifiedBy": {
        "user": {
          "email": "XXXXX@XXXXX.com",
          "id": "b0465821-e891-4f44-9e18-27e875f1b75d",
          "displayName": "XXXXX"
        }
      },
      "parentReference": {},
      "contentType": {
        "id": "0x0100E19591A4ECA81542AEA41A6AAFED6781"
      },
      "fields@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('XXXXX.sharepoint.com%2C495435b4-60c3-49b7-8f6e-1d262a120ae5%2C0fad9f67-35a8-4c0b-892e-113084058c0a')/lists('18a725ac-83ef-48fb-a5cb-950ca2378fd0')/items('9')/fields/$entity",
      "fields": {
        "@odata.etag": "\"a69b1840-239d-42ed-9b20-8789761fb06a,3\"",
        "SerialNumber": "20180824-1353-DC6-Generator-A",
        "id": "9"
      }
    },
    {
      "@odata.etag": "\"13f60f9e-1bf2-4803-93b9-c45234963d47,3\"",
      "createdDateTime": "2018-08-25T22:45:55Z",
      "eTag": "\"13f60f9e-1bf2-4803-93b9-c45234963d47,3\"",
      "id": "10",
      "lastModifiedDateTime": "2018-08-25T22:45:55Z",
      "webUrl": "https://XXXXX.sharepoint.com/sites/GeneratorApp/Lists/GenApp/10_.000",
      "createdBy": {
        "user": {
          "email": "XXXXX@XXXXX.com",
          "id": "b0465821-e891-4f44-9e18-27e875f1b75d",
          "displayName": "XXXXX"
        }
      },
      "lastModifiedBy": {
        "user": {
          "email": "XXXXX@XXXXX.com",
          "id": "b0465821-e891-4f44-9e18-27e875f1b75d",
          "displayName": "XXXXX"
        }
      },
      "parentReference": {},
      "contentType": {
        "id": "0x0100E19591A4ECA81542AEA41A6AAFED6781"
      },
      "fields@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('XXXXX.sharepoint.com%2C495435b4-60c3-49b7-8f6e-1d262a120ae5%2C0fad9f67-35a8-4c0b-892e-113084058c0a')/lists('18a725ac-83ef-48fb-a5cb-950ca2378fd0')/items('10')/fields/$entity",
      "fields": {
        "@odata.etag": "\"13f60f9e-1bf2-4803-93b9-c45234963d47,3\"",
        "SerialNumber": "20180824-1416-DC6-Generator-B",
        "id": "10"
      }
    },
    {
      "@odata.etag": "\"00024848-0d4e-4ee8-b018-f1653af2a577,3\"",
      "createdDateTime": "2018-08-25T22:47:30Z",
      "eTag": "\"00024848-0d4e-4ee8-b018-f1653af2a577,3\"",
      "id": "11",
      "lastModifiedDateTime": "2018-08-25T22:47:30Z",
      "webUrl": "https://XXXXX.sharepoint.com/sites/GeneratorApp/Lists/GenApp/11_.000",
      "createdBy": {
        "user": {
          "email": "XXXXX@XXXXX.com",
          "id": "b0465821-e891-4f44-9e18-27e875f1b75d",
          "displayName": "XXXXX"
        }
      },
      "lastModifiedBy": {
        "user": {
          "email": "XXXXX@XXXXX.com",
          "id": "b0465821-e891-4f44-9e18-27e875f1b75d",
          "displayName": "XXXXX"
        }
      },
      "parentReference": {},
      "contentType": {
        "id": "0x0100E19591A4ECA81542AEA41A6AAFED6781"
      },
      "fields@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('XXXXX.sharepoint.com%2C495435b4-60c3-49b7-8f6e-1d262a120ae5%2C0fad9f67-35a8-4c0b-892e-113084058c0a')/lists('18a725ac-83ef-48fb-a5cb-950ca2378fd0')/items('11')/fields/$entity",
      "fields": {
        "@odata.etag": "\"00024848-0d4e-4ee8-b018-f1653af2a577,3\"",
        "SerialNumber": "20180824-1438-DC6-Generator-R",
        "id": "11"
      }
    },
    {
      "@odata.etag": "\"7c8e80ed-6fea-408a-9594-2b7b13e3691b,3\"",
      "createdDateTime": "2018-08-25T23:02:43Z",
      "eTag": "\"7c8e80ed-6fea-408a-9594-2b7b13e3691b,3\"",
      "id": "12",
      "lastModifiedDateTime": "2018-08-25T23:02:43Z",
      "webUrl": "https://XXXXX.sharepoint.com/sites/GeneratorApp/Lists/GenApp/12_.000",
      "createdBy": {
        "user": {
          "email": "XXXXX@XXXXX.com",
          "id": "b0465821-e891-4f44-9e18-27e875f1b75d",
          "displayName": "XXXXX"
        }
      },
      "lastModifiedBy": {
        "user": {
          "email": "XXXXX@XXXXX.com",
          "id": "b0465821-e891-4f44-9e18-27e875f1b75d",
          "displayName": "XXXXX"
        }
      },
      "parentReference": {},
      "contentType": {
        "id": "0x0100E19591A4ECA81542AEA41A6AAFED6781"
      },
      "fields@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('XXXXX.sharepoint.com%2C495435b4-60c3-49b7-8f6e-1d262a120ae5%2C0fad9f67-35a8-4c0b-892e-113084058c0a')/lists('18a725ac-83ef-48fb-a5cb-950ca2378fd0')/items('12')/fields/$entity",
      "fields": {
        "@odata.etag": "\"7c8e80ed-6fea-408a-9594-2b7b13e3691b,3\"",
        "SerialNumber": "20180824-1456-DC6-Generator-C",
        "id": "12"
      }
    }
  ]
}}

好像我现在正在转一圈。

我最后想要的是ID和序列号列表。

[更新代码以反映我的评论]

1 个答案:

答案 0 :(得分:0)

由于我确实回答了该主题的问题,所以我要问的第二件事是一个完全独立的问题。

所以发生的事情是我看错了数据。我正在查看的区域也恰好有一个“ id”字段,因此显示的是“ id”,而不是我真正想要的那个。当我设置适当的入口点时,我可以在相应的字段下进行操作。