How to decode a json response from API

后端 未结 3 464
一向
一向 2021-01-29 06:47

I am calling data from an API like this:

$curl = curl_init();
//adding fields to the curl object to enter the site
curl_setopt($curl, CURLOPT_URL, $my_url);
curl         


        
相关标签:
3条回答
  • 2021-01-29 06:52

    Use json_decode(). If you want to view the php object so you can better understand how to use it, try

    echo '<pre>';
    echo print_r(json_decode($jsonStr, true));
    echo '</pre>';
    
    0 讨论(0)
  • 2021-01-29 06:55

    You can use json_decode() method

    Please refer this link http://php.net/manual/en/function.json-decode.php

    0 讨论(0)
  • 2021-01-29 07:03

    Not sure why is not working for you. When the json string is in the code it does work.

    <?php
    
    $json = '[{"id":1,"name":"Books","description":null,"reference":null,"status":"active","category":{"id":"5048","name":"Ventas"},"price":[{"idPriceList":"1","name":"General","type":"amount","price":"200.0000"}],"tax":[]},{"id":2,"name":"pencil","description":null,"reference":null,"status":"active","category":{"id":"5048","name":"Ventas"},"price":[{"idPriceList":"1","name":"General","type":"amount","price":"5000.0000"}],"tax":[]}]';
    
    $arr = json_decode($json, true);
    
    $id = $arr[0]['id'];
    $name = $arr[0]['name'];
    print("$id $name\n");
    

    Ouput:

    1 Books
    

    Btw my version

    PHP 5.6.30 (cli) (built: Aug  8 2017 12:20:45)
    
    0 讨论(0)
提交回复
热议问题