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 に答える