从UWP的JSON Odata列表中获取项
我一直很难弄明白,我现在几乎把头发都扯掉了 使用这段代码(添加了一个我用stoppoint查看的var结果): 我只想将userage、UserName和UserTitle的值分别放入文本框中,但不确定如何将它们取出 我很确定我需要建立某种类型的类,但正是@odata部分让我伤透了脑筋 我尝试过的每件事都会返回一个空值。我在那里看到了值,只是不确定如何解析/提取它 我已经看了(更新):从UWP的JSON Odata列表中获取项,json,uwp,odata,Json,Uwp,Odata,我一直很难弄明白,我现在几乎把头发都扯掉了 使用这段代码(添加了一个我用stoppoint查看的var结果): 我只想将userage、UserName和UserTitle的值分别放入文本框中,但不确定如何将它们取出 我很确定我需要建立某种类型的类,但正是@odata部分让我伤透了脑筋 我尝试过的每件事都会返回一个空值。我在那里看到了值,只是不确定如何解析/提取它 我已经看了(更新): 但随后我遇到了一个问题,即有两个[JsonProperty(@odata.etag”)。将[JsonPrope
但随后我遇到了一个问题,即有两个[JsonProperty(@odata.etag”)。将
[JsonProperty]
自定义属性添加到实际持有该值的C#属性中。您不需要将属性放在Title
或createDateTime
属性上,而需要将它们放在它们自己的属性上:
public class RootObject
{
[JsonProperty("@odata.context")]
public string ODataContext { get; set; }
[JsonProperty("@odata.etag")]
public string ODataETag { get; set; }
// No attribute needed here
public DateTime createdDateTime { get; set; }
// etc...
此外,您试图将内容解析为字段
类,但它是根对象
;你需要使用
JsonConvert.DeserializeObject<SharePointListItems.RootObject>(content)
JsonConvert.DeserializeObject(内容)
获取对象。我更新了上面的代码块。我的var结果现在提取了正确的数据,但值为空。谢谢,先生。一旦我做了那个小小的更改,然后将返回值更改为result.fields.UserName,我就收到了所需的结果。非常感谢。
using Newtonsoft.Json;
using System;
public class SharePointListItems
{
public class UserCreated
{
public string email { get; set; }
public string id { get; set; }
public string displayName { get; set; }
}
public class CreatedBy
{
public UserCreated user { get; set; }
}
public class UserModified
{
public string email { get; set; }
public string id { get; set; }
public string displayName { get; set; }
}
public class LastModifiedBy
{
public UserModified user { get; set; }
}
public class ParentReference
{
}
public class ContentType
{
public string id { get; set; }
}
public class Fields
{
[JsonProperty("@odata.etag")]
public string ODataETag { get; set; }
public string Title { get; set; }
public string UserName { get; set; }
public string UserAge { get; set; }
public string UserTitle { get; set; }
}
public class RootObject
{
[JsonProperty("@odata.context")]
public string ODataContext { get; set; }
[JsonProperty("@odata.etag")]
public string ODataETag { get; set; }
public DateTime createdDateTime { get; set; }
public string eTag { get; set; }
public string id { get; set; }
public DateTime lastModifiedDateTime { get; set; }
public string webUrl { get; set; }
public CreatedBy createdBy { get; set; }
public LastModifiedBy lastModifiedBy { get; set; }
public ParentReference parentReference { get; set; }
public ContentType contentType { get; set; }
[JsonProperty("fields@odata.context")]
public string FieldsODataContext { get; set; }
public Fields fields { get; set; }
}
}
public class RootObject
{
[JsonProperty("@odata.context")]
public string ODataContext { get; set; }
[JsonProperty("@odata.etag")]
public string ODataETag { get; set; }
// No attribute needed here
public DateTime createdDateTime { get; set; }
// etc...
JsonConvert.DeserializeObject<SharePointListItems.RootObject>(content)