【发布时间】:2012-12-05 18:30:59
【问题描述】:
使用 C# 和 Visual Studio 2010(Windows 窗体项目)、InstaSharp 和 Newtonsoft.Json 库。
当我请求特定主题标签时,我想从 Endpoint Instagram API 返回给我的 JSON 字符串中获取 图像 url。
到目前为止,我可以检索 JSON 字符串。
我正在尝试使用 Newtonsoft.Json 使用示例反序列化对象,但我可能无法正确理解对象的 JSON 字符串表示。
下面是一个简化的示例response,我从他们的文档中的 api 调用tags/tag-name/media/recent 获得。 source here
{
"data": [{
"type": "image",
"filter": "Earlybird",
"tags": ["snow"],
"comments": {
}
"caption": {
},
"likes": {
},
"created_time": "1296703536",
"images": {
"low_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/02/f9443f3443484c40b4792fa7c76214d5_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/02/f9443f3443484c40b4792fa7c76214d5_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/02/f9443f3443484c40b4792fa7c76214d5_7.jpg",
"width": 612,
"height": 612
}
},
"id": "22699663",
"location": null
},
...
]
}
我想在images 部分中专门获取standard_resolution。
这是我目前拥有的相关代码。
//Create the Client Configuration object using Instasharp
var config = new InstaSharp.Endpoints.Tags.Unauthenticated(config);
//Get the recent pictures of a particular hashtag (tagName)
var pictures = config.Recent(tagName);
//Deserialize the object to get the "images" part
var pictureResultObject = JsonConvert.DeserializeObject<dynamic>(pictureResult.Json);
consoleTextBox.Text = pictureResult.Json;
var imageUrl = pictureResultObject.Data.Images;
Console.WriteLine(imageUrl);
我收到错误:Additional information: Cannot perform runtime binding on a null reference
所以imageUrl 在我调试时确实为空,因此表明我没有以正确的方式访问它。
谁能向我解释如何使用 Newtonsoft.Json 访问这个 JSON 字符串的不同部分?
【问题讨论】:
标签: c# json instagram json.net