Json_decode不起作用

时间:2015-12-06 05:01:42

标签: php json curl

我可以看到我的json格式化响应但是一旦我尝试解码并打印特定值,那么它只是静默而不打印错误/ null。尝试了几乎所有访问json的方法。错误报告在

$url = 'localhost:8080/app/api/Api.php?name=c';
$client = curl_init($url);
curl_setopt($client, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($client);
$result = json_decode($response, true); // without true tried
echo $response; //prints json response
echo $result->count; // does not work here $result['count'] tried

JSON回复如下:

{"status":200,"message":"data found","data":{"count":"1050"}}

3 个答案:

答案 0 :(得分:1)

你可以做这样的事情来获得个人价值,

$url = 'localhost:8080/app/api/Api.php?name=c';
$client = curl_init($url);
curl_setopt($client, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($client);
$result = json_decode($response, true);
echo $result['status'] . "<br />";  // 200
echo $result['message'] . "<br />"; // data found
echo $result['data']['count'] . "<br />";  // 1050

输出:

200
data found
1050

答案 1 :(得分:0)

在json_decode()中,您通过将2nd参数设置为true来告诉函数创建关联数组而不是对象。要使它成为一个对象,只需使用:

$result = json_decode($response);

否则,使用您当前的方法,您可以使用$ result [&#39; count&#39;];

来访问变量

答案 2 :(得分:0)

json_decode函数具有布尔值,当TRUE返回时,对象将被转换为关联数组。因此,如果您想使用对象而不是数组,请删除TRUE

#include "stdafx.h"

#include "Music.h"
#include "Video.h"
#include "MediaManager.h"

int main() {
    using namespace lab6;

    try {
        // Create MediaManager First
        std::unique_ptr<MediaManager> pMediaManager;
        pMediaManager.reset( new MediaManager );

        // Create A Few Media Items
        Music m1( "Wouldn't it Be Nice", 3.40f );
        Music m2( "My Eyes Adore You", 4.20f );
        Music m3( "Comfortably Numb", 6.8f );

        Video v1( "Shawshank Redemption", 142.5f );
        Video v2( "Matrix", 148.2f );
        Video v3( "Lord of the Rings: Fellowship Of the Ring", 190.5f );
        Video v4( "Lord of the Rings: The Two Towers", 188.4f );
        Video v5( "Lord of the Rings: Return of the King", 191.2f );

        // Now Add these to the Manager which encapsulates the queue
        pMediaManager->addToQueue( dynamic_cast<Media*>( &v3 ) );
        pMediaManager->addToQueue( dynamic_cast<Media*>( &v4 ) );
        pMediaManager->addToQueue( dynamic_cast<Media*>( &v5 ) );
        pMediaManager->addToQueue( dynamic_cast<Media*>( &m2 ) );
        pMediaManager->addToQueue( dynamic_cast<Media*>( &v2 ) );
        pMediaManager->addToQueue( dynamic_cast<Media*>( &m3 ) );
        pMediaManager->addToQueue( dynamic_cast<Media*>( &v1 ) );
        pMediaManager->addToQueue( dynamic_cast<Media*>( &m1 ) );

        // Now as A Test Will Get Both The Front Then The Back
        std::cout << "Display Front & Back Of Queue" << std::endl;
        pMediaManager->getFront()->display();
        pMediaManager->getBack()->display();
        std::cout << std::endl << std::endl;

        // Now Will Will Show Full Queue Which Will Also Pop The Queue As It Goes Through the List
        std::cout << "Display Entire Queue: " << std::endl;
        pMediaManager->displayQueue();
        std::cout << std::endl << std::endl;

        // Now Our Queue Should Be Empty
        std::cout << "Is Queue Empty: " << std::endl;
        std::cout << (pMediaManager->isEmpty() ? "true" : "false") << std::endl;
        std::cout << std::endl << std::endl;

        pMediaManager.reset();

    } catch( const std::string& str ) {
        std::cout << "Exception Thrown: " << str << std::endl;
        std::cout << "Press any key to quit." << std::endl;
        _getch();
        return RETURN_ERROR;
    } catch( ... ) {
        std::cout << __FUNCTION__ << " Caught Unknown Exception" << std::endl;
        std::cout << "Press any key to quit." << std::endl;
        _getch();
        return RETURN_ERROR;
    } 

    return RETURN_OK;
} // main

或者您可以使用数组而不是对象

$url = 'localhost:8080/app/api/Api.php?name=c';
$client = curl_init($url);
curl_setopt($client, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($client);
$result = json_decode($response);
echo $response; //prints json response
echo $result->count; // should work