使用 Json.net 的 Instagram API
Posted
技术标签:
【中文标题】使用 Json.net 的 Instagram API【英文标题】:Instagram API using Json.net 【发布时间】:2014-05-11 15:36:19 【问题描述】:我正在开发一个桌面程序,基本上我只是想收集有关计时器或循环的信息,因此每次循环运行时,它都会从 Instagram 提供的 JSON 中保存一个 ID,然后转到网络上,然后以此类推,我决定仅在提供的 Json instagram 中的一张图片上进行测试,如下所示。
"pagination":"next_max_tag_id":"1399847337728968","deprecation_warning":"next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead","next_max_id":"1399847337728968","next_min_id":"1399847337793336","min_tag_id":"1399847337793336","next_url":"https:\/\/api.instagram.com\/v1\/tags\/snow\/media\/recent?access_token=+49749273.8a6c775.3e8cf314d75045e49ba17263d0bededa\u0026count=1\u0026max_tag_id=1399847337728968","meta":"code":200,"data":["attribution":null,"tags":["ilovenorway","hiking","spring","sunny","blaaskjerdigen","bluebird","cloud","clouds","redbullnorge","sun","visit","snow","aalesund","sunnm\u00f8rsalps","nofilter","norway","redbull"],"type":"image","location":"latitude":62.595061667,"longitude":6.735158333,"comments":"count":0,"data":[],"filter":"Normal","created_time":"1399822137","link":"http:\/\/instagram.com\/p\/n3JFAHGTdZ\/","likes":"count":0,"data":[],"images":"low_resolution":"url":"http:\/\/origincache-ash.fbcdn.net\/10326375_690380867685736_1411474740_a.jpg","width":306,"height":306,"thumbnail":"url":"http:\/\/origincache-ash.fbcdn.net\/10326375_690380867685736_1411474740_s.jpg","width":150,"height":150,"standard_resolution":"url":"http:\/\/origincache-ash.fbcdn.net\/10326375_690380867685736_1411474740_n.jpg","width":640,"height":640,"users_in_photo":["position":"y":0.040625,"x":0.06535948,"user":"username":"redbullnorge","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_184192460_75sq_1342379531.jpg","id":"184192460","full_name":"Red Bull Norge","position":"y":0.9609375,"x":0.06535948,"user":"username":"redbull","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_476322_75sq_1334010289.jpg","id":"476322","full_name":"Red Bull"],"caption":"created_time":"1399822137","text":"#nofilter #blaaskjerdigen #bluebird #cloud #clouds #sun #sunny #hiking #snow #spring #ilovenorway #redbull #redbullnorge #visit #norway #aalesund #sunnm\u00f8rsalps","from":"username":"jooakimandersen","profile_picture":"http:\/\/images.ak.instagram.com\/profiles \/profile_1246631612_75sq_1396821408.jpg","id":"1246631612","full_name":"Joakim Andersen","id":"718082592168556034","user_has_liked":false,"id":"718082591723960153_1246631612","user":"username":"jooakimandersen","website":"","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_1246631612_75sq_1396821408.jpg","full_name":"Joakim Andersen","bio":"","id":"1246631612"]
我正在尝试从这个 json 文件中获取信息,如下所示
WebClient webClient = new WebClient();
var list = webClient.DownloadString("https://api.instagram.com/v1/tags/likeforlike/media/recent?access_token=" + Variables.authtoken + "&count=1");
dynamic parsedList = JsonConvert.DeserializeObject<dynamic>(list);
MessageBox.Show(parsedList.data.count.ToString());
但我无法从中得到任何东西,它会加载
MessageBox.Show(parsedList.data.ToString());
很好,但我无法更深入地了解信息
【问题讨论】:
dynamic
非常有用。然而,人们总是试图在它不属于它的情况下使用它。不要使用dynamic
,而是要自己思考是否更适合创建类。答案通常是“是”。方便的是,有一个工具可以为您从 JSON 创建 C# 类,Craig 已经为您演示了。
【参考方案1】:
使用 json2csharp (http://json2csharp.com) 我们生成了以下类。
public class Pagination
public string next_max_tag_id get; set;
public string deprecation_warning get; set;
public string next_max_id get; set;
public string next_min_id get; set;
public string min_tag_id get; set;
public string next_url get; set;
public class Meta
public int code get; set;
public class Location
public double latitude get; set;
public double longitude get; set;
public class Comments
public int count get; set;
public List<object> data get; set;
public class Likes
public int count get; set;
public List<object> data get; set;
public class LowResolution
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 StandardResolution
public string url get; set;
public int width get; set;
public int height get; set;
public class Images
public LowResolution low_resolution get; set;
public Thumbnail thumbnail get; set;
public StandardResolution standard_resolution get; set;
public class Position
public double y get; set;
public double x get; set;
public class User
public string username get; set;
public string profile_picture get; set;
public string id get; set;
public string full_name get; set;
public class UsersInPhoto
public Position position get; set;
public User user 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 Caption
public string created_time get; set;
public string text get; set;
public From from get; set;
public string id get; set;
public class User2
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 Datum
public object attribution get; set;
public List<string> tags get; set;
public string type get; set;
public Location 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<UsersInPhoto> users_in_photo get; set;
public Caption caption get; set;
public bool user_has_liked get; set;
public string id get; set;
public User2 user get; set;
public class RootObject
public Pagination pagination get; set;
public Meta meta get; set;
public List<Datum> data get; set;
然后您应该能够使用 JSON.NET 反序列化为该结构。
var root = JsonConvert.DeserializeObject<RootObject>(list);
此时只需拉取您需要的属性。
【讨论】:
谢谢你完美的工作。我读错了结构。感谢您的帮助! 如果它解决了您的问题,请考虑通过单击答案旁边的复选标记将其标记为答案。谢谢。以上是关于使用 Json.net 的 Instagram API的主要内容,如果未能解决你的问题,请参考以下文章
当我使用 python-instagram 库时,instagram api keep raise 'You must provide a client_id' exception
调用 Instagram 'www.instagram.com/explore/tags/tag/?__a=1' 返回 Instagram 登录页面