1

I have this JSON:

{
    "total": 23695,
    "total_pages": 1185,
    "results": [{
        "id": "r7bVvV7MLdQ",
        "created_at": "2018-01-17T06:38:03-05:00",
        "updated_at": "2018-05-09T03:35:24-04:00",
        "width": 4032,
        "height": 2526,
        "color": "#F7EDE7",
        "description": null,
        "urls": {
            "raw": "https://images.unsplash.com/photo-1516189050082-44d4deb5ceef?ixlib=rb-0.3.5\u0026ixid=eyJhcHBfaWQiOjEyMDd9\u0026s=8b6caac6353f390fbbabde8441dd1959",
            "full": "https://images.unsplash.com/photo-1516189050082-44d4deb5ceef?ixlib=rb-0.3.5\u0026q=85\u0026fm=jpg\u0026crop=entropy\u0026cs=srgb\u0026ixid=eyJhcHBfaWQiOjEyMDd9\u0026s=89ca725623d794116d3741907c93ceab",
            "regular": "https://images.unsplash.com/photo-1516189050082-44d4deb5ceef?ixlib=rb-0.3.5\u0026q=80\u0026fm=jpg\u0026crop=entropy\u0026cs=tinysrgb\u0026w=1080\u0026fit=max\u0026ixid=eyJhcHBfaWQiOjEyMDd9\u0026s=666eb6ac25c7fec68d5994545b933726",
            "small": "https://images.unsplash.com/photo-1516189050082-44d4deb5ceef?ixlib=rb-0.3.5\u0026q=80\u0026fm=jpg\u0026crop=entropy\u0026cs=tinysrgb\u0026w=400\u0026fit=max\u0026ixid=eyJhcHBfaWQiOjEyMDd9\u0026s=3dbc611c97d323ff8b4b043cff19317b",
            "thumb": "https://images.unsplash.com/photo-1516189050082-44d4deb5ceef?ixlib=rb-0.3.5\u0026q=80\u0026fm=jpg\u0026crop=entropy\u0026cs=tinysrgb\u0026w=200\u0026fit=max\u0026ixid=eyJhcHBfaWQiOjEyMDd9\u0026s=0c760185c35eadb31a7bba9b9794d424"
        },
        "links": {
            "self": "https://api.unsplash.com/photos/r7bVvV7MLdQ",
            "html": "https://unsplash.com/photos/r7bVvV7MLdQ",
            "download": "https://unsplash.com/photos/r7bVvV7MLdQ/download",
            "download_location": "https://api.unsplash.com/photos/r7bVvV7MLdQ/download"
        },
        "categories": [],
        "sponsored": false,
        "likes": 0,
        "liked_by_user": false,
        "current_user_collections": [],
        "slug": "cloud-smoke-steam",
        "user": {
            "id": "G69mdFHx0X0",
            "updated_at": "2018-05-03T14:00:07-04:00",
            "username": "maxkuk",
            "name": "Max Kukurudziak",
            "first_name": "Max",
            "last_name": "Kukurudziak",
            "twitter_username": null,
            "portfolio_url": "http://www.instagram.com/makckuk",
            "bio": "Product Designer at MacPaw, Lecturer at Projector",
            "location": "Kiev, Ukraine",
            "links": {
                "self": "https://api.unsplash.com/users/maxkuk",
                "html": "https://unsplash.com/@maxkuk",
                "photos": "https://api.unsplash.com/users/maxkuk/photos",
                "likes": "https://api.unsplash.com/users/maxkuk/likes",
                "portfolio": "https://api.unsplash.com/users/maxkuk/portfolio",
                "following": "https://api.unsplash.com/users/maxkuk/following",
                "followers": "https://api.unsplash.com/users/maxkuk/followers"
            },
            "profile_image": {
                "small": "https://images.unsplash.com/profile-1518780839522-ee199eceaf8c?ixlib=rb-0.3.5\u0026q=80\u0026fm=jpg\u0026crop=faces\u0026cs=tinysrgb\u0026fit=crop\u0026h=32\u0026w=32\u0026s=c37d2f2844b45f52c0f66cd580a200c8",
                "medium": "https://images.unsplash.com/profile-1518780839522-ee199eceaf8c?ixlib=rb-0.3.5\u0026q=80\u0026fm=jpg\u0026crop=faces\u0026cs=tinysrgb\u0026fit=crop\u0026h=64\u0026w=64\u0026s=93647049c20b6a323870fe0886eee329",
                "large": "https://images.unsplash.com/profile-1518780839522-ee199eceaf8c?ixlib=rb-0.3.5\u0026q=80\u0026fm=jpg\u0026crop=faces\u0026cs=tinysrgb\u0026fit=crop\u0026h=128\u0026w=128\u0026s=aff2d13afe9fe418b562b85c226b7e8e"
            },
            "instagram_username": "makckuk",
            "total_collections": 0,
            "total_likes": 6,
            "total_photos": 56
        },
        "tags": [{
            "title": "cloud"
        },
        {
            "title": "smoke"
        },
        {
            "title": "steam"
        },
        {
            "title": "mountain"
        },
        {
            "title": "volcano"
        },
        {
            "title": "blue"
        },
        {
            "title": "rock"
        },
        {
            "title": "glacier"
        },
        {
            "title": "field"
        },
        {
            "title": "geysir"
        },
        {
            "title": "iceland"
        }],
        "photo_tags": [{
            "title": "cloud"
        },
        {
            "title": "smoke"
        },
        {
            "title": "steam"
        },
        {
            "title": "mountain"
        },
        {
            "title": "volcano"
        },
        {
            "title": "blue"
        },
        {
            "title": "rock"
        },
        {
            "title": "glacier"
        },
        {
            "title": "field"
        },
        {
            "title": "geysir"
        },
        {
            "title": "iceland"
        }]
    },

I need to get results.profile_image.small I tried it on many ways but I never figured out how to access to profile_image fields.

Basically I want to do something like this:

dynamic array = JsonConvert.DeserializeObject(responz);

foreach (var itemx in array["results"])
{
    MessageBox.Show(itemx.profile_image.small.ToString()); 
}

I spent last few hours figuring it out, searching searching StackOverflow. The last option is do this with regex which would be very stupid thing to do.

2
  • 2
    profile_image lives inside the user object, so just change your code to itemx.user.profile_image.small.ToString(). However this would be way better if you deserialized into a concrete class instead of using dynamic. You likely would not have had this issue if you had intelisense backing you up. Commented May 15, 2018 at 16:53
  • @maccettura Thank You! Commented May 15, 2018 at 16:55

1 Answer 1

1

Based on your JSON, the actual path should be:

itemx.user.profile_image.small

So if you modify your code to include the missing "user" portion:

dynamic array = JsonConvert.DeserializeObject(responz);

foreach (var itemx in array["results"])
{
    MessageBox.Show(itemx.user.profile_image.small.ToString()); 
}

That should solve your problem.

However, this problem would likely not have presented itself if you were using a concrete class to deserialize into. You would have type safety and the assistance of Intelisense if you use a concrete class. You can easily convert your JSON sample into a concrete class using Json2CSharp or using the "Paste as Class" function of modern Visual Studio versions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.