2016-12-30 9 views
0

を表示していない結果私はJSONの下からシンボルを解析しようとしています:ここ読書JSON_decode -

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

例応答:私は希望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 
    }] 
} 

ループを介してシンボル(すなわち、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>"; 
 
}