反序列化 json 容器数组

Deserialize a json container array

我试图反序列化的数据在 post>

中完全像这样 json

Deserialize JSON Facebook feed using Gson ,只有更多的对象,在容器内..我没有使用 "Gson"

我认为有些对象甚至在某处嵌套了自己的容器,但这不是目前的问题,主要问题是我在反序列化为以下 class..

public class Rootobject
{
    public int found { get; set; }
    public Post[] posts { get; set; }
    public Meta meta { get; set; }
}

我了解 API 中的 json 的布局方式,它是某种数组,在 Json.net 文档中,他们称其为 DataSet.. 我我正在使用 Json.Net

我最近的反序列化尝试是;

List<Rootobject> data = JsonConvert.DeserializeObject<List<Rootobject>>(stringData);

但数据仍然为空,我得到;

附加信息:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[WebApplication17.Models.Rootobject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

也许 LINQ to JSON 是目前还不确定的方式。有什么建议吗?

首先是 post 处的 JSON 不是有效的 JSON 字符串,因此无法将其反序列化。其次,我尝试纠正和更正 JSON 以使其成为有效的 JSON,因此您的 JSON 看起来像

{
    "data": [
        {
            "id": "105458566194298_411506355589516",
            "message": "...",
            "type": "status",
            "created_time": "2012-11-25T17:26:41+0000",
            "updated_time": "2012-11-25T17:26:41+0000",
            "comments": {
                "count": 0
            }
        },
        {
            "id": "105458566194298_411506355589516",
            "message": "...",
            "type": "status",
            "created_time": "2012-11-25T17:26:41+0000",
            "updated_time": "2012-11-25T17:26:41+0000",
            "comments": {
                "count": 0
            }
        }
    ]
}

所以 类 结构应该是这样的

public class Comments
{
    public int count { get; set; }
}
public class Datum
{
    public string id { get; set; }
    public string message { get; set; }
    public string type { get; set; }
    public DateTime created_time { get; set; }
    public DateTime updated_time { get; set; }
    public Comments comments { get; set; }
}
public class MyData
{
    public List<Datum> data { get; set; }
}

然后像您一样正常地反序列化

MyData data = JsonConvert.DeserializeObject<MyData>(stringData);