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

时间:2012-02-09 12:39:13

标签: c# asp.net facebook json.net

我有以下功能,它获取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 Response的示例:

{
  "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
    }
  ]
}
]
}

我只想枚举列表并创建逗号分隔列表并将其保存到db。任何人都可以提出一些需要做的事情吗?如何获取列表的计数和长度属性?

2 个答案:

答案 0 :(得分:5)

两件事:

1)将JsonObjectAttribute添加到NewsFeedPost

[JsonObject]
public class NewsFeedPost : IEnumerable<Post>

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

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

然后你会把你的deseralize称为你希望反序列化的类:

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

答案 1 :(得分:0)

您可能想尝试对DeserializeObject进行强制转换

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