反序列化JSON来类实现在Asp.net的IEnumerable [英] Deserialize Json to Class that implements Ienumerable in Asp.net

查看:67
本文介绍了反序列化JSON来类实现在Asp.net的IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下函​​数获取Facebook数据并返回为字符串。

I've below function which fetches facebook data and return it as string.

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

这里的code调用此函数,并将其转换使用Json.NET到Jobject:

Here's the code that calls this function and convert it to Jobject using Json.NET :

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

下面一行它反序列化为NewsFeedPost类:

Below line deserialize it to NewsFeedPost Class :

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

Now problem is when I change my NewsFeedPost class to below it throws error "Cannot deserialize JSON object into type '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响应的示例:

Below is the sample of 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
    }
  ]
}
]
}

我只是想通过列表枚举,并创建以逗号分隔的列表,并保存到数据库中。任何人都可以抛出一些光需要做什么呢?我怎样才能获得伯爵和长度物业列表?

I just want to enumerate through list and create comma delimited list and save it to db. Can anybody throw some light what needs to be done? How can I get the Count and Length Property for List?

推荐答案

两件事情:

1)添加 JsonObjectAttribute NewsFeedPost

[JsonObject]
public class NewsFeedPost : IEnumerable<Post>

2)反序列化JSON你展示,你需要另一个类:

2) To deserialize that JSON which you show, you need another class:

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

,然后你会打电话与您的类作为deseralize你想要什么样的反序列化:

and then you'd call your deseralize with that class as what you want deserialized:

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

这篇关于反序列化JSON来类实现在Asp.net的IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆