UWP使用JSON数据创建列表

时间:2018-08-26 22:58:20

标签: c# json uwp

好。我已将GET请求发送到SharePoint并收到一个字符串:

"{\"@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\"}}]}"

JObject.Parse解析到哪个:

{{
  "@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"
      }
    }
  ]
}}

我最终想要做的是创建一个填充有Seri​​alNumber的下拉列表。在下拉列表中选择序列号后,它将返回ID,以便我可以将其插入GET请求中以检索适当的列表项。

我正在尝试确定是否需要进行foreach来创建LIST <>或其他所有内容。

我确实有此类设置,但是不确定是否可以按照我认为的方式使用它。

public class Lookup
{
    string id { get; set; };
    string SerialNumber { get; set; }
}

这是最终的工作代码:

    private async void GetButton_Click(object sender, RoutedEventArgs e)
    {
        var (authResult, message) = await Authentication.AquireTokenAsync();
        ResultText.Text = message;

        if (authResult != null)
        {
            var httpClient = new HttpClient();
            HttpResponseMessage response;
            var request = new HttpRequestMessage(HttpMethod.Get, geturl);
            //Add the token in Authorization header
            request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
            response = await httpClient.SendAsync(request);
            var content = await response.Content.ReadAsStringAsync();
            JObject json = JObject.Parse(content);
            var result = JsonConvert.DeserializeObject<SharePointListItems.RootObject>(content);
            foreach (var d in result.value)
            {
                Lookups.Add(new SharePointListItems.Lookup() { id = d.fields.id, SerialNumber = d.fields.SerialNumber });
            }
            TestComboBox.ItemsSource = Lookups;
        }
    }

    private void TestComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (TestComboBox.SelectedIndex != -1)
        {
            var mylookupId = (TestComboBox.SelectedItem as SharePointListItems.Lookup).id;// get your id and do further processing here.
            ResultText.Text = mylookupId;
        }
    }

public class SharePointListItems
{
    public class Lookup
    {
        public string SerialNumber { get; set; }
        public string id { get; set; }
        public override string ToString()
        {
            return SerialNumber;
        }
    }

    public class Value
    {
        public Lookup fields { get; set; }
    }

    public class Fields
    {
        [JsonProperty("@odata.etag")]
        public string ODataETag { 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; }

        public List<Value> value { get; set; }
    }    
}

3 个答案:

答案 0 :(得分:2)

有两种方法可以轻松地创建模型。

  1. 您可以在Visual Studio中使用Web Essentials,使用“编辑”>“选择性粘贴”>将JSON粘贴为类,可以更轻松地了解Json与模型之间的关系。

  2. 如果您不能使用Web Essentials,则可以代替使用http://json2csharp.com/在线JSON到Model类。

您可以尝试使用这些模型来携带JSON格式。

public class Lookup
{
    public string SerialNumber { get; set; }
    public string id { get; set; }
}

public class Value
{
    public Lookup fields { get; set; }
}

public class RootObject
{
    public List<Value> value { get; set; }
}

然后,您可以直接使用obj.value属性集合。

var obj = JsonConvert.DeserializeObject<RootObject>(jsonData);
foreach (var item in obj.value)
{
    //item.fields.id
    //item.fields.SerialNumber
}

答案 1 :(得分:1)

此处是如何将数据列表绑定到组合框(下拉列表)的一个很好的示例:https://www.c-sharpcorner.com/article/data-binding-in-xaml-uwp-using-combobox/

在此示例中,该类是具有 id Name 学生,它显示了如何在组合框中显示姓名。

  

我会针对您的情况进行一些修改,但是如果您想深入了解,可以访问上面提供的链接。

     

这是课程

public class Fields
{
    public string SerialNumber { get; set; }
    public string id { get; set; }
    public override string ToString() 
    {  
        return this.SerialNumber; // so that we can just bind to the object and get serial number in the ui. 
    }
}  
  

将项目添加到列表的后端

public sealed partial class MainPage: Page 
{  
    List<Lookup> Lookups = new List<Lookup>();  
    public MainPage() 
    {  
        this.InitializeComponent();  
        Lookups.Add(new Lookup() {id = 1, SerialNumber = "S1"});  
        Lookups.Add(new Lookup() {id = 2, SerialNumber = "S2"}); 
        Lookups.Add(new Lookup() {id = 3, SerialNumber = "S3"});  
        Lookups.Add(new Lookup() {id = 4, SerialNumber = "S4"}); 
        //add as many items here as u want, u can even use a for loop or foreach loop or a Deserializer with newsoft json to get objects from ur json like below.
        //var data = JsonConvert.DeserializeObject<RootObject>(jsonData);
        //foreach (var d in data.value)
        //{
        //    //d.fields.id //this is how u can get the inside properties.
        //}
        yourComboBox.ItemSource = Lookups;//setting item source to UI.
    } 
}
  

成功将数据与UI绑定后,可以使用组合框的 SelectionChanged 事件根据需要执行进一步的逻辑,

void MyComboBox_SelectionChanged(object sender, object args)
{
    if(MyCombobox.SelectedIndex!=-1)
    {
        var mylookupId = (MyCombobox.SelectedItem as Lookup).id;// get your id and do further processing here.
    }
}

答案 2 :(得分:0)

我认为使用1[T] callback) [0x00050] in <4b0c1fc6e5a94482990701acc6dec8b3>:0 at RestSharp.Http.ResponseCallback (System.IAsyncResult result, System.Action会很容易:

newtonsoft

免责声明:未经任何测试;-)