好。我已将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\":\"[email protected]\",\"id\":\"b0465821-e891-4f44-9e18-27e875f1b75d\",\"displayName\":\"XXXXX\"}},\"lastModifiedBy\":{\"user\":{\"email\":\"[email protected]\",\"id\":\"b0465821-e891-4f44-9e18-27e875f1b75d\",\"displayName\":\"XXXXX\"}},\"parentReference\":{},\"contentType\":{\"id\":\"0x0100E19591A4ECA81542AEA41A6AAFED6781\"},\"[email protected]\":\"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\":\"[email protected]\",\"id\":\"b0465821-e891-4f44-9e18-27e875f1b75d\",\"displayName\":\"XXXXX\"}},\"lastModifiedBy\":{\"user\":{\"email\":\"[email protected]\",\"id\":\"b0465821-e891-4f44-9e18-27e875f1b75d\",\"displayName\":\"XXXXX\"}},\"parentReference\":{},\"contentType\":{\"id\":\"0x0100E19591A4ECA81542AEA41A6AAFED6781\"},\"[email protected]\":\"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\":\"[email protected]\",\"id\":\"b0465821-e891-4f44-9e18-27e875f1b75d\",\"displayName\":\"XXXXX\"}},\"lastModifiedBy\":{\"user\":{\"email\":\"[email protected]\",\"id\":\"b0465821-e891-4f44-9e18-27e875f1b75d\",\"displayName\":\"XXXXX\"}},\"parentReference\":{},\"contentType\":{\"id\":\"0x0100E19591A4ECA81542AEA41A6AAFED6781\"},\"[email protected]\":\"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\":\"[email protected]\",\"id\":\"b0465821-e891-4f44-9e18-27e875f1b75d\",\"displayName\":\"XXXXX\"}},\"lastModifiedBy\":{\"user\":{\"email\":\"[email protected]\",\"id\":\"b0465821-e891-4f44-9e18-27e875f1b75d\",\"displayName\":\"XXXXX\"}},\"parentReference\":{},\"contentType\":{\"id\":\"0x0100E19591A4ECA81542AEA41A6AAFED6781\"},\"[email protected]\":\"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": "[email protected]", "id": "b0465821-e891-4f44-9e18-27e875f1b75d", "displayName": "XXXXX" } }, "lastModifiedBy": { "user": { "email": "[email protected]", "id": "b0465821-e891-4f44-9e18-27e875f1b75d", "displayName": "XXXXX" } }, "parentReference": {}, "contentType": { "id": "0x0100E19591A4ECA81542AEA41A6AAFED6781" }, "[email protected]": "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": "[email protected]", "id": "b0465821-e891-4f44-9e18-27e875f1b75d", "displayName": "XXXXX" } }, "lastModifiedBy": { "user": { "email": "[email protected]", "id": "b0465821-e891-4f44-9e18-27e875f1b75d", "displayName": "XXXXX" } }, "parentReference": {}, "contentType": { "id": "0x0100E19591A4ECA81542AEA41A6AAFED6781" }, "[email protected]": "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": "[email protected]", "id": "b0465821-e891-4f44-9e18-27e875f1b75d", "displayName": "XXXXX" } }, "lastModifiedBy": { "user": { "email": "[email protected]", "id": "b0465821-e891-4f44-9e18-27e875f1b75d", "displayName": "XXXXX" } }, "parentReference": {}, "contentType": { "id": "0x0100E19591A4ECA81542AEA41A6AAFED6781" }, "[email protected]": "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": "[email protected]", "id": "b0465821-e891-4f44-9e18-27e875f1b75d", "displayName": "XXXXX" } }, "lastModifiedBy": { "user": { "email": "[email protected]", "id": "b0465821-e891-4f44-9e18-27e875f1b75d", "displayName": "XXXXX" } }, "parentReference": {}, "contentType": { "id": "0x0100E19591A4ECA81542AEA41A6AAFED6781" }, "[email protected]": "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的下拉列表。在下拉列表中选择SerialNumber时,它将返回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("[email protected]")] public string FieldsODataContext { get; set; } public Fields Fields { get; set; } public List<Value> value { get; set; } }} 最佳答案 如何将数据列表绑定到组合框(下拉列表)的一个很好的示例在这里:https://www.c-sharpcorner.com/article/data-binding-in-xaml-uwp-using-combobox/在此示例中,该类是具有id和Name的Student,它显示了如何在组合框中显示姓名。  我将针对您的情况对其进行一些修改,但是如果您想深入了解,可以访问上面提供的链接。    这是班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. }}关于c# - UWP使用JSON数据创建列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52030850/
10-13 03:21