0
    namespace WebApplication1.Site
{
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        var accessToken = "" // Token
        var client = new FacebookClient(accessToken);
        dynamic myInfo = client.Get("me/friends", new { fields = "name,id,work" });

        foreach (dynamic friend in myInfo)
        {
            foreach (dynamic work in friend.work ?? new[] { new { employer = new { name = string.Empty }, position = new { name = string.Empty } } })
                {
                    Response.Write("Employer: " + work.employer.name);
                }

        }


    }
}
}

21行目で次のエラーが発生しています。原因がわかりません。

'System.Collections.Generic.KeyValuePair'には'work'の定義が含まれていません

FacebookGraphAPIからのサンプルJSONリターン。これは最初の3人の友達だけです。私が解析している4000人近くの友人がいます。明らかに、これはデータの構造のコンテキストを提供します。

    {
      "data": [
{
  "name": "Mia xxx", 
  "id": "11381", 
  "work": [
    {
      "employer": {
        "id": "100982923276720", 
        "name": "New-York Historical Society"
      }, 
      "location": {
        "id": "108424279189115", 
        "name": "New York, New York"
      }
    }
  ]
}, 
{
  "name": "Leilah xxx", 
  "id": "1133"
}, 
{
  "name": "xxx, 
  "id": "1231", 
  "work": [
    {
      "employer": {
        "id": "104362369673437", 
        "name": "Bye Bye Liver: The Philadelphia Drinking Play"
      }, 
      "location": {
        "id": "101881036520836", 
        "name": "Philadelphia, Pennsylvania"
      }, 
      "position": {
        "id": "121113421241569", 
        "name": "Actress/Bartender"
      }, 
      "description": "A sketch comedy/improv show every Saturday night at Downey's on South & Front. Come thirsty!", 
      "start_date": "2011-09"
    }, 
    {
      "employer": {
        "id": "100952634348", 
        "name": "Act II Playhouse"
      }, 
      "location": {
        "id": "109249869093538", 
        "name": "Ambler, Pennsylvania"
      }, 
      "position": {
        "id": "125578900846788", 
        "name": "My Fair Lady"
      }, 
      "description": "11 actor version of the classic musical.", 
      "start_date": "0000-00"
    }, 
4

2 に答える 2

2

これを見てください:

Response.Write("Employer: " + myInfo.work.employer.name);

私はあなたが意味したと思う:

Response.Write("Employer: " + work.employer.name);

このように言います-それがあなたの意図したものではないwork場合、あなたの変数の目的は何ですか?

于 2012-08-03T20:30:14.287 に答える
2

An alternative to relying on the dynamic is to capture and parse the JSON with JSON.net, it's designed for querying json data and is really much safer than using dynamic

http://json.codeplex.com/

And deserializing into classes:

http://dotnetbyexample.blogspot.ca/2012/02/json-deserialization-with-jsonnet-class.html

于 2012-08-03T20:49:24.407 に答える