检索“图像".从Instagram API检索的JSON字符串中 [英] Retrieving "images" from a JSON string retrived from Instagram API

查看:122
本文介绍了检索“图像".从Instagram API检索的JSON字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#和Visual Studio 2010(Windows窗体项目),InstaSharp和Newtonsoft.Json库.

Using C# and Visual Studio 2010 (Windows Form Project), InstaSharp and Newtonsoft.Json libraries.

我想从 Endpoint Instagram API返回的JSON字符串中获取图片网址,当我请求特定的主题标签时.

I want to get the image url from the JSON string returned to me by the Endpoint Instagram API when I request for a particular hashtag.

到目前为止,我可以检索JSON字符串.

I can so far retrive the JSON string.

我正在尝试使用 Newtonsoft.Json 通过示例反序列化对象,但是我可能无法正确理解对象的JSON字符串表示形式.

I am trying to use Newtonsoft.Json to deserialize the object using the examples, but I probably dont understand the JSON string representation of the object properly.

以下是我从其文档的api调用tags/tag-name/media/recent获得的简化示例response. 在此处获取

Below is a simplified sample response I get from the api call tags/tag-name/media/recent from their documentation. 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.

I want to get specifically the standard_resolution in the images part.

这是我目前拥有的Revelevant代码.

This is the revelevant code that I currently have.

//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在调试时确实为空,因此表明我没有以正确的方式访问它.

so imageUrl is indeed null when I debug, hence indicating I am not accessing it the right way.

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

Anyone can explain to me how to access different parts of this JSON String using Newtonsoft.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);
}

这篇关于检索“图像".从Instagram API检索的JSON字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆