0

我有一个 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属性之外,一切都通过了。我应该如何映射这些?

4

2 回答 2

1
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);

试试下面的代码:)

于 2012-08-17T20:30:15.940 回答
0

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

然后可以使用 JSON.Net 进行反序列化:ex.JsonConvert.DeserializeObject(input) iirc

于 2012-08-17T20:15:43.567 回答