c# - 从从 Instagram API 检索到的 JSON 字符串中检索 "images"

标签 c# json instagram json.net

使用 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);

我收到错误:附加信息:无法对空引用执行运行时绑定(bind)

因此 imageUrl 在我调试时确实为 null,因此表明我没有以正确的方式访问它。

任何人都可以向我解释如何使用 Newtonsoft.Json 访问此 JSON 字符串的不同部分?

最佳答案

使用 Newtonsoft.Json

dynamic dyn = JsonConvert.DeserializeObject(json);
foreach (var data in dyn.data)
{
    Console.WriteLine("{0} - {1}",
            data.filter,
            data.images.standard_resolution.url);
}

关于c# - 从从 Instagram API 检索到的 JSON 字符串中检索 "images",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13729997/

相关文章:

c# - 可观察到 Rx 中的回调

ios - 在 Swift 中读取 JSON Key 0

json - 无法修复 Active Record 挂起的迁移错误

ios - 通过 instagram API 删除 instagram 媒体上的评论

c# - botbuilder 不等待来自 HTTP 响应的响应(使用 Restsharp)

c# - EF 6 Code First,使用 Include on navigation 属性更改外键 ID 会导致 "A referential integrity constraint violation occurred"错误

c# - 在 C# 中将 Xml 元素复制到另一个文档

java - 如何使用 Gson、Retrofit、Active Android 将嵌套 JSON 对象反序列化为字符串

php - 如何获取用户的 Instagram 提要

facebook - Instagram 新客户注册被禁用