2014-03-04 212 views
3

我試圖從Instagram API獲取JSON,並將我的時間軸上的人員的姓名放在使用Xamarin.iOS的UITableView上。我試圖使用JSON.Net和Restsharp來做到這一點,但是這個問題在過去的幾周裏從未解決。這裏是我如何嘗試檢索我的數據,與此問題是有一個錯誤消息:System.InvalidCastexception已被拋出不能從源類型轉換爲目標類型。無法加載JSON數據

在此之上我將如何加載一個特定的元素到表上?

我已經把它改成JObject.Parse(response.content); ,現在我收到此錯誤: enter image description here 這裏是我更新的代碼:

client.ExecuteAsync(request, response => { 
       var array = JObject.Parse(response.Content); 
       foreach (JObject obj in array) 
        {  
         var Name = (string)obj["data"]["user"]["full_name"]; 
         var Caption = (string)obj["data"]["caption"]["text"]; 
         var picURL = (string)obj["data"]["images"]["low_resolution"]["url"]; 

         System.Console.WriteLine("Name of " + Name + ", Caption of " + Caption + ", and picurl of " + picURL); 
        } 
      }); 

這裏是JSON響應:

{ 
    "pagination": { 
     "next_url": "https://api.instagram.com/v1/users/self/feed?access_token=6489401.88b3fb2.7af2a0355ea24f4590efa1ee82ed0a49&max_id=668439962408115415_16915182", 
     "next_max_id": "668439962408115415_16915182" 
    }, 
    "meta": { 
     "code": 200 
    }, 
    "data": [ 
     { 
      "attribution": null, 
      "tags": [ 
       "selfie" 
      ], 
      "type": "image", 
      "location": null, 
      "comments": { 
       "count": 0, 
       "data": [] 
      }, 
      "filter": "Normal", 
      "created_time": "1393953134", 
      "link": "http://instagram.com/p/lIO1_jmkkr/", 
      "likes": { 
       "count": 14, 
       "data": [ 
        { 
         "username": "muahjay", 
         "profile_picture": "http://images.ak.instagram.com/profiles/profile_304620904_75sq_1392618687.jpg", 
         "id": "304620904", 
         "full_name": "Jeanettee Nicole Cambero Gamez" 
        }, 
        { 
         "username": "iam_mrsmith31", 
         "profile_picture": "http://images.ak.instagram.com/profiles/profile_32806209_75sq_1392607367.jpg", 
         "id": "32806209", 
         "full_name": "Kiondrix Smith" 
        }, 
        { 
         "username": "anniyalation", 
         "profile_picture": "http://images.ak.instagram.com/profiles/profile_30752906_75sq_1390523572.jpg", 
         "id": "30752906", 
         "full_name": "Niya G." 
        }, 
        { 
         "username": "fonzo_badmon", 
         "profile_picture": "http://images.ak.instagram.com/profiles/profile_271402803_75sq_1393641486.jpg", 
         "id": "271402803", 
         "full_name": "Slimshady" 
        } 
       ] 
      }, 
      "images": { 
       "low_resolution": { 
        "url": "http://distilleryimage11.s3.amazonaws.com/e02592b0a3bf11e3b63212d269f676eb_6.jpg", 
        "width": 306, 
        "height": 306 
       }, 
       "thumbnail": { 
        "url": "http://distilleryimage11.s3.amazonaws.com/e02592b0a3bf11e3b63212d269f676eb_5.jpg", 
        "width": 150, 
        "height": 150 
       }, 
       "standard_resolution": { 
        "url": "http://distilleryimage11.s3.amazonaws.com/e02592b0a3bf11e3b63212d269f676eb_8.jpg", 
        "width": 640, 
        "height": 640 
       } 
      }, 
      "users_in_photo": [], 
      "caption": { 
       "created_time": "1393953134", 
       "text": "#Selfie", 
       "from": { 
        "username": "kthompkins7", 
        "profile_picture": "http://images.ak.instagram.com/profiles/profile_5941681_75sq_1378508674.jpg", 
        "id": "5941681", 
        "full_name": "kthompkins7" 
       }, 
       "id": "668849828018145517" 
      }, 
      "user_has_liked": false, 
      "id": "668849827690989867_5941681", 
      "user": { 
       "username": "kthompkins7", 
       "website": "", 
       "profile_picture": "http://images.ak.instagram.com/profiles/profile_5941681_75sq_1378508674.jpg", 
       "full_name": "kthompkins7", 
       "bio": "", 
       "id": "5941681" 
      } 
     }, 
     { 
      "attribution": null, 
      "tags": [ 
       "rp", 
       "amen" 
      ], 
      "type": "image", 
      "location": null, 
      "comments": { 
       "count": 5, 
       "data": [ 
        { 
         "created_time": "1393949795", 
         "text": "@tmcmc Dobson a Christian!!!!", 
         "from": { 
          "username": "keelanwillison", 
          "profile_picture": "http://images.ak.instagram.com/profiles/profile_26089241_75sq_1380511035.jpg", 
          "id": "26089241", 
          "full_name": "Keelanwillison" 
         }, 
         "id": "668821819276579152" 
        }, 
        { 
         "created_time": "1393950029", 
         "text": "Yo my cousin got me a singed autograph from you at the Boston boat show thanks man @a_dobson3", 
         "from": { 
          "username": "danthebigboy914", 
          "profile_picture": "http://images.ak.instagram.com/profiles/profile_27354625_75sq_1393859632.jpg", 
          "id": "27354625", 
          "full_name": "Danny Chiappetta" 
         }, 
         "id": "668823778033324461" 
        }, 
        { 
         "created_time": "1393950659", 
         "text": "Amen", 
         "from": { 
          "username": "sandyrodr", 
          "profile_picture": "http://images.ak.instagram.com/profiles/profile_44889389_75sq_1361591427.jpg", 
          "id": "44889389", 
          "full_name": "sandyrodr" 
         }, 
         "id": "668829062428545715" 
        }, 
        { 
         "created_time": "1393950664", 
         "text": "", 
         "from": { 
          "username": "cherydaily", 
          "profile_picture": "http://images.ak.instagram.com/profiles/profile_18483862_75sq_1379884114.jpg", 
          "id": "18483862", 
          "full_name": "Andrew \"Drew\" Chery" 
         }, 
         "id": "668829109488636596" 
        }, 
        { 
         "created_time": "1393951921", 
         "text": "Amen", 
         "from": { 
          "username": "bostonsonia", 
          "profile_picture": "http://images.ak.instagram.com/profiles/profile_182219303_75sq_1391293289.jpg", 
          "id": "182219303", 
          "full_name": "Sonia" 
         }, 
         "id": "668839648625348773" 
        } 
       ] 
      }, 
      "filter": "Normal", 
      "created_time": "1393949714", 
      "link": "http://instagram.com/p/lIIUdggtOj/", 
      "likes": { 
       "count": 324, 
       "data": [ 
        { 
         "username": "bwest05", 
         "profile_picture": "http://images.ak.instagram.com/profiles/profile_387702554_75sq_1393739891.jpg", 
         "id": "387702554", 
         "full_name": "bb" 
        }, 
        { 
         "username": "babygirl6193", 
         "profile_picture": "http://images.ak.instagram.com/profiles/profile_23365060_75sq_1384972330.jpg", 
         "id": "23365060", 
         "full_name": "babygirl6193" 
        }, 
        { 
         "username": "derekmooney1", 
         "profile_picture": "http://images.ak.instagram.com/profiles/profile_218356294_75sq_1388601101.jpg", 
         "id": "218356294", 
         "full_name": "Derek Mooney" 
        }, 
        { 
         "username": "zay0613", 
         "profile_picture": "http://images.ak.instagram.com/profiles/profile_342812999_75sq_1369576584.jpg", 
         "id": "342812999", 
         "full_name": "Zay" 
        } 
       ] 
      }, 
      "images": { 
       "low_resolution": { 
        "url": "http://distilleryimage11.s3.amazonaws.com/0e1b270aa3b811e395af12cdc849cb9b_6.jpg", 
        "width": 306, 
        "height": 306 
       }, 
       "thumbnail": { 
        "url": "http://distilleryimage11.s3.amazonaws.com/0e1b270aa3b811e395af12cdc849cb9b_5.jpg", 
        "width": 150, 
        "height": 150 
       }, 
       "standard_resolution": { 
        "url": "http://distilleryimage11.s3.amazonaws.com/0e1b270aa3b811e395af12cdc849cb9b_8.jpg", 
        "width": 640, 
        "height": 640 
       } 
      }, 
      "users_in_photo": [], 
      "caption": { 
       "created_time": "1393949714", 
       "text": "#RP from @flashgoodwin #AMEN", 
       "from": { 
        "username": "a_dobson3", 
        "profile_picture": "http://images.ak.instagram.com/profiles/profile_203045179_75sq_1374262647.jpg", 
        "id": "203045179", 
        "full_name": "a_dobson3" 
       }, 
       "id": "668821135462420769" 
      }, 
      "user_has_liked": false, 
      "id": "668821135110099875_203045179", 
      "user": { 
       "username": "a_dobson3", 
       "website": "", 
       "profile_picture": "http://images.ak.instagram.com/profiles/profile_203045179_75sq_1374262647.jpg", 
       "full_name": "a_dobson3", 
       "bio": "", 
       "id": "203045179" 
      } 
     }, 
     { 
      "attribution": null, 
      "tags": [ 
       "tb12" 
      ], 
      "type": "image", 
      "location": null, 
      "comments": { 
       "count": 169, 
       "data": [ 
        { 
         "created_time": "1393952642", 
         "text": "My guy! The greatest! I just want him to get another ring so the haters can stop saying he hasn't won one since 04. I mean he still won them right? #TomBrady @patriots", 
         "from": { 
          "username": "k_aus32", 
          "profile_picture": "http://images.ak.instagram.com/profiles/profile_39840771_75sq_1390431094.jpg", 
          "id": "39840771", 
          "full_name": "k_aus32" 
         }, 
         "id": "668845703585318559" 
        }, 
        { 
         "created_time": "1393952673", 
         "text": "@heres_jonni9", 
         "from": { 
          "username": "allyson21lautner", 
          "profile_picture": "http://images.ak.instagram.com/profiles/profile_184895764_75sq_1393107648.jpg", 
          "id": "184895764", 
          "full_name": "Allyson " 
         }, 
         "id": "668845962264823470" 
        }, 
        { 
         "created_time": "1393952694", 
         "text": "Let's go !", 
         "from": { 
          "username": "tom12terrific", 
          "profile_picture": "http://images.ak.instagram.com/profiles/profile_421023272_75sq_1393644166.jpg", 
          "id": "421023272", 
          "full_name": "Andrew Estrada" 
         }, 
         "id": "668846135581853361" 
        }, 
        { 
         "created_time": "1393952856", 
         "text": "♡", 
         "from": { 
          "username": "in_neverland74", 
          "profile_picture": "http://images.ak.instagram.com/profiles/profile_181245582_75sq_1391928288.jpg", 
          "id": "181245582", 
          "full_name": "Karen" 
         }, 
         "id": "668847491935880949" 
        }, 
        { 
         "created_time": "1393952914", 
         "text": "Follow for patriots pics daily!! (Ifollowback)", 
         "from": { 
          "username": "ne_patriots_fanpage__", 
          "profile_picture": "http://images.ak.instagram.com/profiles/profile_983242248_75sq_1391989748.jpg", 
          "id": "983242248", 
          "full_name": "New England Patriots Fanpage" 
         }, 
         "id": "668847980295472912" 
        }, 
        { 
         "created_time": "1393952952", 
         "text": "Best ever; period. Let's see anyone win a ring with the humps he's had to work with, (except Randy).", 
         "from": { 
          "username": "calidoso76", 
          "profile_picture": "http://images.ak.instagram.com/profiles/profile_1101426299_75sq_1392654362.jpg", 
          "id": "1101426299", 
          "full_name": "calidoso76" 
         }, 
         "id": "668848301545604900" 
        }, 
        { 
         "created_time": "1393953005", 
         "text": "Shit just bcuz brady hasnt won a ring since 04 dont mean a thing....one more and he'll have 6.....the most superbowl wins than ANY quarterback EVER. @patriots", 
         "from": { 
          "username": "inkandartsosick", 
          "profile_picture": "http://images.ak.instagram.com/profiles/profile_260242163_75sq_1363337609.jpg", 
          "id": "260242163", 
          "full_name": "Eric Hughes" 
         }, 
         "id": "668848744304723771" 
        }, 
        { 
         "created_time": "1393953023", 
         "text": "", 
         "from": { 
          "username": "bvsed_papi_", 
          "profile_picture": "http://images.ak.instagram.com/profiles/profile_408657565_75sq_1393905141.jpg", 
          "id": "408657565", 
          "full_name": "Abel Valle™" 
         }, 
         "id": "668848894913791811" 
        } 
       ] 
      }, 
      "filter": "Lo-fi", 
      "created_time": "1393949267", 
      "link": "http://instagram.com/p/lIHd-_v8Uj/", 
      "likes": { 
       "count": 13685, 
       "data": [ 
        { 
         "username": "dollathebarber", 
         "profile_picture": "http://images.ak.instagram.com/profiles/profile_219503241_75sq_1373905843.jpg", 
         "id": "219503241", 
         "full_name": "dollathebarber" 
        }, 
        { 
         "username": "emastro23", 
         "profile_picture": "http://images.ak.instagram.com/profiles/profile_21122956_75sq_1388823730.jpg", 
         "id": "21122956", 
         "full_name": "Eddie Mastrocola" 
        }, 
        { 
         "username": "pat_fan99", 
         "profile_picture": "http://images.ak.instagram.com/profiles/profile_304601453_75sq_1377084414.jpg", 
         "id": "304601453", 
         "full_name": "Timothy Smith" 
        }, 
        { 
         "username": "___adam___q", 
         "profile_picture": "http://images.ak.instagram.com/profiles/profile_3694047_75sq_1367384028.jpg", 
         "id": "3694047", 
         "full_name": "Adam Quinonez" 
        } 
       ] 
      }, 
      "images": { 
       "low_resolution": { 
        "url": "http://distilleryimage10.s3.amazonaws.com/1c67f514a3b711e383121299eef1f922_6.jpg", 
        "width": 306, 
        "height": 306 
       }, 
       "thumbnail": { 
        "url": "http://distilleryimage10.s3.amazonaws.com/1c67f514a3b711e383121299eef1f922_5.jpg", 
        "width": 150, 
        "height": 150 
       }, 
       "standard_resolution": { 
        "url": "http://distilleryimage10.s3.amazonaws.com/1c67f514a3b711e383121299eef1f922_8.jpg", 
        "width": 640, 
        "height": 640 
       } 
      }, 
      "users_in_photo": [], 
      "caption": { 
       "created_time": "1393949267", 
       "text": "#tb12", 
       "from": { 
        "username": "patriots", 
        "profile_picture": "http://images.ak.instagram.com/profiles/profile_1939080_75sq_1385924433.jpg", 
        "id": "1939080", 
        "full_name": "New England Patriots" 
       }, 
       "id": "668817391815214218" 
      }, 
      "user_has_liked": false, 
      "id": "668817391496447267_1939080", 
      "user": { 
       "username": "patriots", 
       "website": "", 
       "profile_picture": "http://images.ak.instagram.com/profiles/profile_1939080_75sq_1385924433.jpg", 
       "full_name": "New England Patriots", 
       "bio": "", 
       "id": "1939080" 
      } 
     }, 
     { 
      "attribution": null, 
      "videos": { 
       "low_resolution": { 
        "url": "http://distilleryimage4.s3.amazonaws.com/2cb7114aa3b511e3a3e312f545262070_102.mp4", 
        "width": 480, 
        "height": 480 
       }, 
       "standard_resolution": { 
        "url": "http://distilleryimage4.s3.amazonaws.com/2cb7114aa3b511e3a3e312f545262070_101.mp4", 
        "width": 640, 
        "height": 640 
       } 
      } 
     } 
    ] 
} 
+2

什麼行引發異常? – Jason

+0

var arr = JsonConvert.DeserializeObject (response.Content); – Prad

+0

嘗試解析內容而不是 – Nilesh

回答

5

根據您的JSON響應(我已經更新了它的驗證目的從jsonlint.com),似乎json數據是一個對象類型而不是數組類型。所以,你的JSON響應的適當分類後,你更新的代碼可能再次如下更新

client.ExecuteAsync(request, response => 
    { 
     RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(response.Content); 
     foreach (Datum obj in rootObject.data) 
     { 
      var Name = obj.user.full_name; 
      var Caption = obj.caption.text; 
      var picURL = obj.images.low_resolution.url; 

      System.Console.WriteLine("Name of " + Name + ", Caption of " + Caption + ", and picurl of " + picURL); 
     } 
    } 
); 

,哪些是你可以從你的JSON響應派生的類像下面已經從json2csharp.com

產生
public class RootObject 
{ 
    public Pagination pagination { get; set; } 
    public Meta meta { get; set; } 
    public List<Datum> data { get; set; } 
} 

public class Datum 
{ 
    public object attribution { get; set; } 
    public List<string> tags { get; set; } 
    public string type { get; set; } 
    public object location { get; set; } 
    public Comments comments { get; set; } 
    public string filter { get; set; } 
    public string created_time { get; set; } 
    public string link { get; set; } 
    public Likes likes { get; set; } 
    public Images images { get; set; } 
    public List<object> users_in_photo { get; set; } 
    public Caption caption { get; set; } 
    public bool user_has_liked { get; set; } 
    public string id { get; set; } 
    public User user { get; set; } 
    public Videos videos { get; set; } 
} 

public class Videos 
{ 
    public LowResolution2 low_resolution { get; set; } 
    public StandardResolution2 standard_resolution { get; set; } 
} 

public class StandardResolution2 
{ 
    public string url { get; set; } 
    public int width { get; set; } 
    public int height { get; set; } 
} 

public class LowResolution2 
{ 
    public string url { get; set; } 
    public int width { get; set; } 
    public int height { get; set; } 
} 

public class User 
{ 
    public string username { get; set; } 
    public string website { get; set; } 
    public string profile_picture { get; set; } 
    public string full_name { get; set; } 
    public string bio { get; set; } 
    public string id { get; set; } 
} 

public class Caption 
{ 
    public string created_time { get; set; } 
    public string text { get; set; } 
    public From from { get; set; } 
    public string id { get; set; } 
} 

public class From 
{ 
    public string username { get; set; } 
    public string profile_picture { get; set; } 
    public string id { get; set; } 
    public string full_name { get; set; } 
} 

public class Images 
{ 
    public LowResolution low_resolution { get; set; } 
    public Thumbnail thumbnail { get; set; } 
    public StandardResolution standard_resolution { get; set; } 
} 

public class StandardResolution 
{ 
    public string url { get; set; } 
    public int width { get; set; } 
    public int height { get; set; } 
} 

public class Thumbnail 
{ 
    public string url { get; set; } 
    public int width { get; set; } 
    public int height { get; set; } 
} 

public class LowResolution 
{ 
    public string url { get; set; } 
    public int width { get; set; } 
    public int height { get; set; } 
} 

public class Likes 
{ 
    public int count { get; set; } 
    public List<Datum2> data { get; set; } 
} 

public class Datum2 
{ 
    public string username { get; set; } 
    public string profile_picture { get; set; } 
    public string id { get; set; } 
    public string full_name { get; set; } 
} 

public class Comments 
{ 
    public int count { get; set; } 
    public List<object> data { get; set; } 
} 

public class Meta 
{ 
    public int code { get; set; } 
} 

public class Pagination 
{ 
    public string next_url { get; set; } 
    public string next_max_id { get; set; } 
} 
+0

謝謝你的幫助!但最後一件事,我將如何顯示這到UITableView?所有現有的代碼都可以順利運行。 – Prad

+0

你也歡迎!很高興知道它爲你工作。順便說一句,你可以開始一個單獨的線程與你的新查詢,以便你可以快速得到你想要的答案。祝你好運! –

+0

+1爲json2csharp鏈接 – Askolein