如何使用JSON.NET将JSON反序列化为.NET对象



我有一个类似以下的JSON对象

{
   "data": [
      {
         "id": "18128270850211_49239570772655",
         "from": {
            "name": "Someone Unimportant",
            "id": "57583427"
         }
         /* more stuff */
      }
   ]
}

我想使用JSON.NET、来解析它

FacebookResponse<FacebookPost> response = JsonConvert.DeserializeObject<FacebookResponse<FacebookPost>>(json);
internal class FacebookResponse<T> where T : class
{
    public IList<T> Data { get; set; }
    public FacebookResponsePaging Paging { get; set; }
}
public class FacebookPost
{
    public string Id { get; set; }
    [JsonProperty("to.data.id")]
    public string FeedId { get; set; }
    [JsonProperty("from.id")]
    public string UserId { get; set; }
    [JsonProperty("created_time")]
    public DateTime CreatedTime { get; set; }
    [JsonProperty("updated_time")]
    public DateTime UpdatedTime { get; set; }
    public string Type { get; set; } // TODO: Type enum??
    public string Message { get; set; }
    public string Link { get; set; }
    public string Name { get; set; }
    public string Caption { get; set; }
    public string Description { get; set; }
}

除了FeedIdUserId属性之外,所有内容都通过了。我应该如何映射这些?

public class From
{
    public string name { get; set; }
    public string id { get; set; }
}
public class Datum
{
    public string id { get; set; }
    public From from { get; set; }
}
public class FacebookPost
{
    public List<Datum> data { get; set; }
}
internal class FacebookResponse<T> where T : class
{
    public IList<T> Data { get; set; }
    public FacebookResponsePaging Paging { get; set; }
}
FacebookResponse<FacebookPost> response = JsonConvert.DeserializeObject<FacebookResponse<FacebookPost>>(json);

尝试以下代码:)

使用此网站获取.net 的对象

然后您可以使用JSON.Net进行反序列化:例如JsonConvert.DescializeObject(input)iirc

相关内容

  • 没有找到相关文章

最新更新