2016-12-30 93 views
0

我试图解析从下面的JSON符号:阅读JSON_decode在PHP - 结果不显示

$trending_url = "https://api.stocktwits.com/api/2/trending/symbols/equities.json"; 

示例响应这里:

{ 
    "response": { 
     "status": 200 
    }, 
    "symbols": [{ 
     "id": 11631, 
     "symbol": "JNUG", 
     "title": "Direxion Daily Jr Gld Mnrs Bull 3X Shrs", 
     "is_following": false 
    }, { 
     "id": 9553, 
     "symbol": "NUGT", 
     "title": "Direxion Daily Gold Miners Bull 3X Shrs", 
     "is_following": false 
    }, { 
     "id": 12100, 
     "symbol": "BABA", 
     "title": "Alibaba", 
     "is_following": false 
    }] 
} 

从上面JSON--我想通过循环提取符号(即JNUG,NUGT,BABA)。这是我做的...

$trending_url_info = json_decode(file_get_contents($trending_url),true); 

echo $trending_url_info->symbols->symbol[0] . "<br>"; 
echo $trending_url_info->response->symbols->symbol[0] . "<br>"; 
echo $trending_url_info->response->symbols[0]->symbol[0] . "<br>"; 

echo $trending_url_info['response']['symbols'][0]['symbol'] . "<br>"; 
echo $trending_url_info['response']['symbols'][0]['symbol'][0] . "<br>"; 
echo $trending_url_info['response']['symbols']['symbol'][0] . "<br>"; 

但是没有一个回声上面给我的符号。我如何有一个循环并从JSON提取中提取符号?任何帮助appreacited ..谢谢。

+0

如果你写$ trending_url_info = json_decode(的file_get_contents($ trending_url),TRUE); 在这个'真'的意思,它会返回关联数组,一旦print_r($ trending_url_info)和检查类型是否它的一个对象或数组 – rahulsm

回答

1

这是正确的访问到这些属性一个简单的演示:

<?php 
$jsonInput = <<<EOT 
{ 
    "response": { 
     "status": 200 
    }, 
    "symbols": [{ 
     "id": 11631, 
     "symbol": "JNUG", 
     "title": "Direxion Daily Jr Gld Mnrs Bull 3X Shrs", 
     "is_following": false 
    }, { 
     "id": 9553, 
     "symbol": "NUGT", 
     "title": "Direxion Daily Gold Miners Bull 3X Shrs", 
     "is_following": false 
    }, { 
     "id": 12100, 
     "symbol": "BABA", 
     "title": "Alibaba", 
     "is_following": false 
    }] 
} 
EOT; 

$jsonData = json_decode($jsonInput); 
foreach ($jsonData->symbols as $symbol) { 
    var_dump($symbol->symbol); 
} 

输出显然是:

string(4) "JNUG" 
string(4) "NUGT" 
string(4) "BABA" 
2

symbols不在response之下。这就是为什么$trending_url_info['response']...没有这些工作。也json_decodetrue给你关联数组不是一个对象。

$trending_url_info['symbols'][0]['symbol']这应该工作。

$trending_url_info = json_decode(file_get_contents($trending_url),true); 

$symbols = array(); 

foreach($trending_url_info['symbols'] as $sym) { 
    $symbols[] = $sym['symbol']; 
} 

echo(json_encode($symbols)); 

$symbols会给[JNUG, NUGT, BABA]

0

$trending_url = { 
 
    "response": { 
 
     "status": 200 
 
    }, 
 
    "symbols": [{ 
 
     "id": 11631, 
 
     "symbol": "JNUG", 
 
     "title": "Direxion Daily Jr Gld Mnrs Bull 3X Shrs", 
 
     "is_following": false 
 
    }, { 
 
     "id": 9553, 
 
     "symbol": "NUGT", 
 
     "title": "Direxion Daily Gold Miners Bull 3X Shrs", 
 
     "is_following": false 
 
    }, { 
 
     "id": 12100, 
 
     "symbol": "BABA", 
 
     "title": "Alibaba", 
 
     "is_following": false 
 
    }] 
 
} 
 

 
foreach($trending_url->symbols as $obj){ 
 
    echo $obj->symbol."<br>"; 
 
}