PHP JSON Decode

Discussion in 'PHP' started by greatlogix, May 26, 2008.

  1. #1
    I have long php variable after decoding JSON string using json_decode function.

    object(stdClass)#1 (3) {
    ["responseData"]=>
    object(stdClass)#2 (2) {
    ["results"]=>
    array(4) {
    [0]=>
    object(stdClass)#3 (8) {
    ["GsearchResultClass"]=>
    string(10) "GwebSearch"
    ["unescapedUrl"]=>
    string(32) "http://www.example.com/Home/"
    ........

    What should be the php syntax to access highlighted portion of string/array.

    i want to access value of this variable ["unescapedUrl"]

    Please help.
     
    greatlogix, May 26, 2008 IP
  2. sylsft

    sylsft Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    We've found it's much easier to use the associative array instead of the object return value. To do this, pass 'true' as the second parameter:

    $array = json_decode( 'string ...', true);
    $unescapedUrl = $array['responseData']['results']['unescapedUrl'];
     
    sylsft, May 27, 2008 IP
  3. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #3
    I have tried this
    
    $json = json_decode($body, true);
    echo $json['responseData']['results']['unescapedUrl'];
    
    PHP:
    It does not show anything. Than I tried following

    
    $json = json_decode($body);
    echo $json->$responseData->results[0]->unescapedUrl;
    
    PHP:
    and get this error: Fatal error: Cannot access empty property

    Any tips?
     
    greatlogix, May 28, 2008 IP
  4. Dagon

    Dagon Active Member

    Messages:
    122
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #4
    
    $json = json_decode($body);
    echo($json->responseData->results[0]->unescapedUrl);
    
    PHP:
    if that doesn't work please post the json encoded string here
     
    Dagon, May 28, 2008 IP
    greatlogix likes this.
  5. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #5
    Thanks Dagon. I just fixed it. I was adding $ sign before "responseData". Your code is perfect. Rep added. Thanks again.
     
    greatlogix, May 28, 2008 IP
  6. gyp

    gyp Banned

    Messages:
    44
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    if ( $job == "done" ) {
    echo "Please message me next time you have something";
    } else {
    echo "I am interested in this job, Please Message me.";
    }
     
    gyp, May 28, 2008 IP
  7. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Eh? This was almost clever, and actually on-topic, in the other thread you posted it in but now it's kinda lame and repetitive and out of place here since this wasn't really a "job" and plus he had already posted saying it works now so..

    EDIT: Wow, you actually posted this and only this in a lot of threads even threads that are just asking for help solving a problem (even when the problem was already solved like this thread). Might I suggest you not act like a spam bot?
     
    zerxer, May 28, 2008 IP