SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: JSON Parse

  1. #1
    SitePoint Member
    Join Date
    May 2010
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JSON Parse

    Hey all, kind of a first timer with JSON parsing and was wondering how I could parse this JSON and select the image URL based on the size value. Thanks ahead of time.

    Code PHP:
    "media": {
                            "photos": {
                                "photo": [
                                    {
                                        "@size": "x",
                                        "$t": "http://photos.petfinder.com/photos/US/FL/FL286/25768801/FL286.25768801-1-x.jpg",
                                        "@id": "1"
                                    },
                                    {
                                        "@size": "fpm",
                                        "$t": "http://photos.petfinder.com/photos/US/FL/FL286/25768801/FL286.25768801-1-fpm.jpg",
                                        "@id": "1"
                                    },
                                    {
                                        "@size": "pn",
                                        "$t": "http://photos.petfinder.com/photos/US/FL/FL286/25768801/FL286.25768801-1-pn.jpg",
                                        "@id": "1"
                                    },
                                    {
                                        "@size": "pnt",
                                        "$t": "http://photos.petfinder.com/photos/US/FL/FL286/25768801/FL286.25768801-1-pnt.jpg",
                                        "@id": "1"
                                    },
                                    {
                                        "@size": "t",
                                        "$t": "http://photos.petfinder.com/photos/US/FL/FL286/25768801/FL286.25768801-1-t.jpg",
                                        "@id": "1"
                                    }
                                ]
                            }

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,900
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Use json_decode with the second parameter set to true to decode the JSON into an associative array and then loop that array like you would any other array.

    Suppose you're looking for size 'fpm', then it would be
    PHP Code:
    $array json_decode($jsontrue);

    $url '';
    foreach (
    $array['media']['photos']['photo'] as $photo)
    {
        if (
    $photo['size'] === 'fpm')
        {
            
    $url $photo['$t'];
        }
    }
    if (
    $url === '')
    {
        
    // no photo found of size 'fpm'

    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •