Parsing values from JSON using php

后端 未结 4 1479
再見小時候
再見小時候 2020-12-22 06:16

I\'m new to PHP and JSON, so hoping someone can help me.

I have a PHP which a 3rd party performs a POST against, delivering JSON data. An example of the data sent wo

相关标签:
4条回答
  • 2020-12-22 06:18

    you have multi levels of objects so use:

    echo($event_json->data->object->fee);
    

    Basically you are echoing fee, which is a member of the object name object, which in turn is the attribute of an object named data, which again in turn is a member of your $event_json object.

    0 讨论(0)
  • 2020-12-22 06:22

    When you do a json_decode, you get an array of this data. So, you can access data like this:

    $event_json['data']['object']['fee']
    

    good luck

    0 讨论(0)
  • 2020-12-22 06:27

    First, I would suggest passing true as the second argument to json_decode. This results in an associative array rather than an object, which makes it a little easier to work with.

    Then, you can do this:

    $fee = $event_json['data']['object']['fee'];
    

    You can chain these square brackets as many times as needed to delve deeper into the array.

    0 讨论(0)
  • 2020-12-22 06:27

    You can quickly acheive this in following manner:

    $body = @file_get_contents('php://input');
    $event_json = json_decode($body, true);
    print $event_json['data']['object']['fee'];
    
    0 讨论(0)
提交回复
热议问题