将Json反序列化为在Asp.net中实现Ienumerable的类

本文关键字:实现 Ienumerable 的类 net Asp Json 反序列化 | 更新日期: 2023-09-27 18:24:05

我有下面的函数,它获取facebook数据并将其作为字符串返回。

public static string GetUserNewsFeed(string strAccessToken)
{
    Dictionary<string, object> PostDetail = new Dictionary<string, object>();
    DateTime CreatedDateTime = DateTime.Today.AddDays(-90);
    var epoch = (CreatedDateTime.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
    PostDetail.Add("Posts", "SELECT post_id,source_id FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed') and (likes.count > 0 or comments.count > 0) and created_time > " + epoch);
    PostDetail.Add("PostComments", "select post_id,fromid from comment where post_id in (select post_id from #Posts)");
    PostDetail.Add("PostLikes", "SELECT object_id,post_id,user_id FROM like WHERE post_id in (select post_id from #Posts)");
    string Json = HttpUtility.UrlEncode(Newtonsoft.Json.JsonConvert.SerializeObject(PostDetail));       
    return RunFQLQuery(Json, strAccessToken);
}

以下是调用此函数并使用Json.NET:将其转换为Jobject的代码

strFeed = FacebookAPI.GetUserNewsFeed(Convert.ToString(Session["access_token"]));
JObject objStreamData = JObject.Parse(strFeed);
var PostResponse = objStreamData.SelectToken("data[0]");

下面的行将其反序列化为NewsFeedPost类:

var Posts = JsonConvert.DeserializeObject<NewsFeedPost>(PostResponse.ToString());

下面是类别:

public class NewsFeedPost 
{      
    public string name { get; set; }
    public List<Post> fql_result_set { get; set; }
}
public class Post
{
    public string post_id { get; set; }
    public string source_id { get; set; }
}

现在的问题是,当我将NewsFeedPost类更改为以下时,它会抛出错误"无法将JSON对象反序列化为类型'BAL.NewsFeedPost'":

public class NewsFeedPost : IEnumerable<Post>
{      
    public string name { get; set; }
    public List<Post> fql_result_set { get; set; }
    public IEnumerator<Post> GetEnumerator()
    {
        foreach (Post item in fql_result_set)
        {
            yield return item;
        }
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

以下是Json回应的样本:

{
  "data": [
{
  "name": "Posts",
  "fql_result_set": [
    {
      "post_id": "1"
    },
    {
      "post_id": "2"
    }
  ]
},
{
  "name": "PostComments",
  "fql_result_set": [
    {
      "post_id": "3",
      "fromid": 4
    },
    {
      "post_id": "5",
      "fromid": 6
    }
  ]
},
{
  "name": "PostLikes",
  "fql_result_set": [
    {
      "object_id": 7,
      "post_id": "8",
      "user_id": 9
    },
    {
      "object_id": 10,
      "post_id": "11",
      "user_id": 12
    }
  ]
}
]
}

我只想通过列表进行枚举,创建逗号分隔的列表并将其保存到数据库中。有人能告诉我们需要做什么吗?如何获取List的Count和Length属性?

将Json反序列化为在Asp.net中实现Ienumerable的类

两件事:

1) 将JsonObjectAttribute添加到NewsFeedPost:

[JsonObject]
public class NewsFeedPost : IEnumerable<Post>

2) 要反序列化您显示的JSON,您需要另一个类:

public class NewsFeedPosts
{
    public List<NewsFeedPost> Data { get; set; }
}

然后你会用这个类调用你的deselalize作为你想要反序列化的东西:

var Posts = JsonConvert.DeserializeObject<NewsFeedPosts>(PostResponse.ToString());

您可能想在DeserializeObject 上尝试强制转换

var Posts = JsonConvert.DeserializeObject<NewsFeedPost>((NewsFeedPost)PostResponse.ToString());