读取facebook页面时出现json问题

本文关键字:json 问题 facebook 读取 | 更新日期: 2023-09-27 18:16:39

我试图用Json阅读facebook墙,但我收到了错误-无法访问Newtonsoft.Json.Linq.JProperty上的子值。有什么想法吗?

string pageInfo = client.DownloadString(string.Format("https://graph.facebook.com/Pokerfreerollpass?access_token={0} ", accessToken));
string pagePosts = client.DownloadString(string.Format("https://graph.facebook.com/Pokerfreerollpass/posts?access_token={0} ", accessToken));
//Console.Write(pagePosts);
var result = JsonConvert.DeserializeObject<dynamic>(pagePosts);
foreach (var item in result)
{
    Console.WriteLine(item["body"]["message"].ToString());
}

Console.WriteLine(item["bo"message"].ToString());行显示错误Bellow我正在添加当我更改最后一行时显示的文本"][

foreach (var item in result.Children())
{
    Console.WriteLine(item.ToString());
}

    "id": "105458566194298_494770977263053",
        "from": {
          "id": "105458566194298",
          "category": "Community",
          "name": "Poker freeroll password"
        },
        "message": "?100 ,?200 ,?500 Redbet Freeroll on RBP'nMore Info: http://goo.g
    l/RMMxY (Boss Media network)'n'nall tourneys under 500 players'n27/05/2013 20:00
     CET ?100 Redbet Freeroll'n28/05/2013 20:00 CET ?200 Redbet Freeroll'n29/05/2013
     20:00 CET ?100 Redbet Freeroll'n30/05/2013 20:00 CET ?500 Redbet Freeroll",
        "privacy": {
          "value": ""
        },
        "type": "status",
        "status_type": "mobile_status_update",
        "created_time": "2013-05-27T17:04:35+0000",
        "updated_time": "2013-05-27T17:04:35+0000",
        "likes": {
          "data": [
            {
              "name": "Carlos Alberto Mendoza Alvarez",
              "id": "1417267896"
            },
            {
              "name": "????? ??????",
UPDATE:
Pageposts.Tostring
 88 on showdown in tournament.. maybe it's is not working in this tournament?
 i need to write to support?","can_remove":false,"created_time":"2013-06-01T1
8:50+0000","like_count":0,"user_likes":false},{"id":"497024067037744_77740391
from":{"id":"105458566194298","category":"Community","name":"Poker freeroll p
word"},"message":"'u041f'u0430'u0432'u0435'u043b 'u0418'u0432'u0430'u043d'u04
u0432  Anyway please contact customer support","message_tags":[{"id":"1000012
69275","name":"'u041f'u0430'u0432'u0435'u043b 'u0418'u0432'u0430'u043d'u043e'
32","type":"user","offset":0,"length":12}],"can_remove":false,"created_time":
13-06-01T19:20:22+0000","like_count":0,"user_likes":false},{"id":"49702406703
4_77744788","from":{"name":"'u041f'u0430'u0432'u0435'u043b 'u0418'u0432'u0430
43d'u043e'u0432","id":"100001204869275"},"message":"Answer from support: '"At
is moment we do not offer any promotion with the features at issue.'"'nSo thi
onus doesn't exists in this tournament.","can_remove":false,"created_time":"2
-06-03T09:31:34+0000","like_count":0,"user_likes":false},{"id":"4970240670377
77745216","from":{"id":"105458566194298","category":"Community","name":"Poker
eeroll password"},"message":"'u041f'u0430'u0432'u0435'u043b 'u0418'u0432'u043
043d'u043e'u0432  ok","message_tags":[{"id":"100001204869275","name":"'u041f'
30'u0432'u0435'u043b 'u0418'u0432'u0430'u043d'u043e'u0432","type":"user","off
":0,"length":12}],"can_remove":false,"created_time":"2013-06-03T13:25:35+0000
like_count":0,"user_likes":false}],"paging":{"cursors":{"after":"Ng==","befor
"Mw=="}}}}],"paging":{"previous":"https:'/'/graph.facebook.com'/1054585661942
/posts?access_token=177257699103893|_qFGs75Mlif-Y2rwvK1RsCs_mMY&limit=25&sinc
370346635&__previous=1","next":"https:'/'/graph.facebook.com'/105458566194298
osts?access_token=177257699103893|_qFGs75Mlif-Y2rwvK1RsCs_mMY&limit=25&until=
0108995"}}

代码:现在是

 client.DownloadString(string.Format("https://graph.facebook.com/Pokerfreerollpass?access_token={0} ", accessToken));
            string pagePosts = client.DownloadString(string.Format("https://graph.facebook.com/Pokerfreerollpass/posts?access_token={0} ", accessToken));
            //Console.Write(pagePosts);
            var result = JsonConvert.DeserializeObject<dynamic>(pagePosts);
           //// dynamic message="";
            foreach (var item in result.Children())
            {
                result = JsonConvert.DeserializeObject<RootObject>(item.ToString());
                var message = result.message.ToString();
                Console.Write(message);
            }
       //     Console.Write(message.ToString());

            Console.Read();
        }
        public class From
        {
            public string id { get; set; }
            public string category { get; set; }
            public string name { get; set; }
        }
        public class Privacy
        {
            public string value { get; set; }
        }
        public class Datum
        {
            public string name { get; set; }
            public string id { get; set; }
        }
        public class Likes
        {
            public List<Datum> data { get; set; }
        }
        public class RootObject
        {
            public string id { get; set; }
            public From from { get; set; }
            public string message { get; set; }
            public Privacy privacy { get; set; }
            public string type { get; set; }
            public string status_type { get; set; }
            public string created_time { get; set; }
            public string updated_time { get; set; }
            public Likes likes { get; set; }
        }
    }
}

读取facebook页面时出现json问题

我没有看到body属性。请尝试使用item["message"].ToString()

如果您有json,您可以为此创建映射类。下面会告诉你如何做到这一点。

var result = JsonConvert.DeserializeObject<RootObject>(item.ToString());
var message = result.message; 

为给定json生成的类来自http://json2csharp.com/

public class From
{
    public string id { get; set; }
    public string category { get; set; }
    public string name { get; set; }
}
public class Privacy
{
    public string value { get; set; }
}
public class Datum
{
    public string name { get; set; }
    public string id { get; set; }
}
public class Likes
{
    public List<Datum> data { get; set; }
}
public class RootObject
{
    public string id { get; set; }
    public From from { get; set; }
    public string message { get; set; }
    public Privacy privacy { get; set; }
    public string type { get; set; }
    public string status_type { get; set; }
    public string created_time { get; set; }
    public string updated_time { get; set; }
    public Likes likes { get; set; }
}

更新:

foreach (var item in result.Children())
{
    var result = JsonConvert.DeserializeObject<RootObject>(item.ToString());
    var message = result.message; 
}