错误:在 asp.net 中解析 facebook JSON 时,未将对象引用设置为对象的实例

发布于 2024-11-29 19:37:34 字数 2271 浏览 2 评论 0原文

我正在使用/重用 json.net 来获取用户及其朋友的 facebook 点赞。我遇到了这个错误:对象引用未设置为对象的实例

在下面的代码中,我已经解析了 facebook json 三次,并且在第三次使用它时引发了这个异常....在 准确地说,methoduserlikes 函数...

我认为问题出在 [var i].........前两次工作正常,但第三次出现异常......

请指出找出问题所在......

        oAuth.AccessTokenGet(Request["code"]);
        global_token = oAuth.Token;
        if (oAuth.Token.Length > 0)
        {

            url = "https://graph.facebook.com/me/likes?access_token=" + oAuth.Token;
            url_friends = "https://graph.facebook.com/me/friends?access_token=" + oAuth.Token;
            string json = oAuth.WebRequest(oAuthFacebook.Method.GET, url, String.Empty);
            string jsonfriends = oAuth.WebRequest(oAuthFacebook.Method.GET, url_friends, String.Empty);
            Label1.Text = oAuth.Token.ToString();

           JObject likes = JObject.Parse(json);
            string id = "";//user id
            string name = "";//user likes
            JObject friends = JObject.Parse(jsonfriends);
            string fid = "";
            foreach (var i in likes["data"].Children())       //Loop through the returned friends
            {
                id = i["id"].ToString().Replace("\"", "");
                name = i["name"].ToString().Replace("\"", "");
                Label3.Text = Label3.Text + "<br/> " + name;
            }
            foreach (var i in friends["data"].Children())
            {
                fid = i["id"].ToString().Replace("\"", "");
                methoduserlikes(fid);              
            } 
        }
    }


}


public void methoduserlikes(string id)
{
    Label4.Text = Label4.Text + "<br/> " + id;
    string flike = "";
    string like_id = "";
    string flike_url = "https://graph.facebook.com/" + id + "/likes?access_token=" + global_token;
    string jsonfriend_like = oAuth.WebRequest(oAuthFacebook.Method.GET, flike_url, String.Empty);
    JObject friend_like = JObject.Parse(jsonfriend_like);

    foreach (var i in friend_like["data"].Children())
   {
       like_id = i["id"].ToString().Replace("\"", "");
        **flike = i["name"].ToString().Replace("\"","");**  here the exception is raised

        Label5.Text = Label5.Text + "<br/> " + flike;
    } 
}

}

I am using/reusing json.net to get facebook likes of a user and his/her friend's like. I am having this error : Object reference not set to an instance of an object

In the following code, I have parsed facebook json three times and I have this exception raised the third time I have used it....in the methoduserlikes function to be precise...

I think the problem is in [var i].........it works fine the first two times but exeception occurs third time.....

kindly point out what seems to be the problem.....

        oAuth.AccessTokenGet(Request["code"]);
        global_token = oAuth.Token;
        if (oAuth.Token.Length > 0)
        {

            url = "https://graph.facebook.com/me/likes?access_token=" + oAuth.Token;
            url_friends = "https://graph.facebook.com/me/friends?access_token=" + oAuth.Token;
            string json = oAuth.WebRequest(oAuthFacebook.Method.GET, url, String.Empty);
            string jsonfriends = oAuth.WebRequest(oAuthFacebook.Method.GET, url_friends, String.Empty);
            Label1.Text = oAuth.Token.ToString();

           JObject likes = JObject.Parse(json);
            string id = "";//user id
            string name = "";//user likes
            JObject friends = JObject.Parse(jsonfriends);
            string fid = "";
            foreach (var i in likes["data"].Children())       //Loop through the returned friends
            {
                id = i["id"].ToString().Replace("\"", "");
                name = i["name"].ToString().Replace("\"", "");
                Label3.Text = Label3.Text + "<br/> " + name;
            }
            foreach (var i in friends["data"].Children())
            {
                fid = i["id"].ToString().Replace("\"", "");
                methoduserlikes(fid);              
            } 
        }
    }


}


public void methoduserlikes(string id)
{
    Label4.Text = Label4.Text + "<br/> " + id;
    string flike = "";
    string like_id = "";
    string flike_url = "https://graph.facebook.com/" + id + "/likes?access_token=" + global_token;
    string jsonfriend_like = oAuth.WebRequest(oAuthFacebook.Method.GET, flike_url, String.Empty);
    JObject friend_like = JObject.Parse(jsonfriend_like);

    foreach (var i in friend_like["data"].Children())
   {
       like_id = i["id"].ToString().Replace("\"", "");
        **flike = i["name"].ToString().Replace("\"","");**  here the exception is raised

        Label5.Text = Label5.Text + "<br/> " + flike;
    } 
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文