1

I am so close given everything I have read. Really I have been working on this and trying to understand this for a couple days. Endless google searches, and php manual reading of examples. I just don't think I have hit that AHA! moment where it clicks with me.

JSON Source

object(stdClass)#1 (2) {
  ["type"]=>
   string(7) "success"
   ["response"]=>
   object(stdClass)#2 (2) {
    ["data"]=>
    object(stdClass)#3 (1) {
     ["songs"]=>
     array(10) {
     [0]=>
     object(stdClass)#4 (2) {
      ["title"]=>
      string(36) "Red Hot Chilly Peppers - Scar Tissue"
      ["time"]=>
      int(1426114367)
    }
    [1]=>
    object(stdClass)#5 (2) {
      ["title"]=>
      string(29) "Rock Solid Radio Station ID 2"
      ["time"]=>
      int(1426114359)
    }
    [2]=>
    object(stdClass)#6 (2) {
      ["title"]=>
      string(32) "Nirvana - Come As You Are (1991)"
      ["time"]=>
      int(1426114150)
         }
}
["message"]=>
string(8) "Complete"
}
}

JSON All Pretty (from validator)

{
"type": "success",
"response": {
    "data": {
        "songs": [
            {
                "title": "I Bet My Life - Imagine Dragons - I Bet My Life -                 Imagine Dragons",
                "time": 1426176140
            },
            {
                "title": "Smashing Pumpkins - Mayonaise",
                "time": 1426175786
            }               
        ]
    },
    "message": "Complete"
}
}

Trying to get just the Title of the first song in the list, instead of all of them that come out.

Here is what my code looks like

<?php
error_reporting(E_ALL);
$str = "http://url/to/JSON";
$json = json_decode($str);

echo $json['type']['response']['data']['songs'][0]->title;

?>

I get this error. Notice: Trying to get property of non-object in /home/WackyShmacky/public_html/ouch/test/example2.php on line 6

Line 6:

echo $json['type']['response']['data']['songs'][0]->title;

This is clearly a problem with correct syntax and formatting, and I have looked at tons of examples, and it still has not clicked with me. Help would be greatly appreciated so I can put this behind me.

2 Answers 2

3

http://codepad.org/hZBrhCZ8

<?php
error_reporting(E_ALL);
$str = file_get_contents("http://url/to/JSON"); // Don't forget to retreive the content of the json form the URL
$json = json_decode($str, true); // Set 2nd parameter to true return an associative array

echo $json['response']['data']['songs'][0]['title']; // Right path using an associative array

?>

Explanation :

When the 2nd parameter of json_decode is set to true the function return an associative array who's really close to the structure of the JSON.

From there all what you got to do is put back the JSON path into an associative php array to access the wanted value !

More info :

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

7 Comments

Tried this, but the result was a blank page. I have gotten a lot of blank pages. Tried putting <pre></pre> in it as well. Hmm...
@JMiller I just edited my post saw that you forgot to retreive the content of the URL you can do it easily by using file_get_contents
Hmm...it is still blank. I do have the url in there, and when the url is accessed by itself in a browser it does indeed pull the json up. I just took out the url from this post as I don't want people going to it. Just going to the url itself results in it looking like this. {"type":"success","response":{"data":{"songs":[{"title":"Jane's Addiction - Been Caught Stealing","time":1426181270},{"title":"Ro
How ever I did notice when I do var_dump($json); it returns null...what!
@JMiller can you give me the output of var_dump($str);
|
0

You need to decode as an associated array and it looks like 'type' and 'response' are at the same level, not nested so you want:

$json = json_decode($str, true);
echo $json['response']['data']['songs'][0]['title'];

To get the type alone:

echo echo $json['type'];

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.