Retrieving “images” from a JSON string retrived fr

2020-06-28 03:07发布

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

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.

I can so far retrive the JSON string.

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.

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
    },
    ...
    ]
}

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

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

I get the error: Additional information: Cannot perform runtime binding on a null reference

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

Anyone can explain to me how to access different parts of this JSON String using Newtonsoft.Json?

2条回答
闹够了就滚
2楼-- · 2020-06-28 03:11

I wrote a plugin for .net which takes care of deserializing the json string and returning a data table. it is still in development but see if it helps. Instagram.NET on Github

查看更多
smile是对你的礼貌
3楼-- · 2020-06-28 03:24

Using Newtonsoft.Json

dynamic dyn = JsonConvert.DeserializeObject(json);
foreach (var data in dyn.data)
{
    Console.WriteLine("{0} - {1}",
            data.filter,
            data.images.standard_resolution.url);
}
查看更多
登录 后发表回答