2014-06-12 3 views
3

저는 몇 시간 동안 JSON 배열의 데이터로 간단한 테이블을 만들려고 노력해 왔습니다.JSON 배열에서 HTML 테이블 만들기

다음은 예제입니다. 왜이 코드가 작동하지 않는지 나는 모른다. 오류 :

"비 객체의 속성을 얻으려고합니다."

$json_string = file_get_contents("https://bittrex.com/api/v1/public/getmarkethistory?market=BTC-HYPER&count=5"); 

$array = json_decode($json_string); 
?> 

<table><tr><th> 
<?php foreach($array as $o): ?> 

<tr> 
    <td><?php $o->result->TimeStamp ?></td> 
</tr> 
<?php endforeach; ?> 
</table> 

형식화하는 대한 json_string의 URL를보고하십시오.

+3

'foreach ($ array-> result as $ o)'와'$ o-> TimeStamp'? 그리고 당신은 또한'echo'를 사용해야합니다 – asprin

+0

당신이 이것을 위해 Jquery를 사용하고 싶다면 다음 링크들이 있습니다 : http://www.zachhunter.com/2010/04/json-objects-to-html-table/, 2 - http://stackoverflow.com/questions/1051061/convert-json-array-to-an-html-table-in-jquery –

답변

1

두 번째 매개 변수를 json_decode($json_string, true)에 추가하여 개체가 아닌 배열로 만들 수 있습니다. 이 예제를 고려하십시오

<?php 
$json_string = file_get_contents("https://bittrex.com/api/v1/public/getmarkethistory?market=BTC-HYPER&count=5"); 
$array = json_decode($json_string, true); 

?> 

<table border="1" cellpadding="10"> 
    <thead><tr><th>Timestamp</th></tr></thead> 
    <tbody> 
    <?php foreach($array['result'] as $key => $value): ?> 
     <tr> 
      <td><?php echo $value['TimeStamp']; ?></td> 
     </tr> 
    <?php endforeach; ?> 
    </tbody> 
</table> 
0
$json_string = file_get_contents("https://bittrex.com/api/v1/public/getmarkethistory?market=BTC-HYPER&count=5"); 

$array = json_decode($json_string); 
?> 

<table> 
<?php foreach($array->result as $o) 
{ 
echo "<tr> 
    <td>".$o->TimeStamp."</td> 
</tr>"; 
} ?> 
</table> 

이렇게하면됩니다. foreach 루프를 $array->result에 설정해야합니다.

0

Trying to get property of non-object 그 속성이 존재하지 않기 때문에 ->property 호출 중 하나가 실패 의미합니다. 이 경우 실패한 $ o- 결과입니다.

print_r($array); 

출력 : 당신이 내부 객체를 얻기 위해이 구조를 따를 수

이제
stdClass Object 
(
    [success] => 1 
    [message] => 
    [result] => Array 
     (
      [0] => stdClass Object 
       (
        [Id] => 10044 
        [TimeStamp] => 2014-06-12T04:36:32.227 
        [Quantity] => 3 
        [Price] => 2.9E-5 
        [Total] => 8.7E-5 
        [FillType] => FILL 
        [OrderType] => BUY 
       ) 

      [1] => stdClass Object 
       (
        [Id] => 10040 
        [TimeStamp] => 2014-06-12T04:23:22.683 
        [Quantity] => 49.9 
        [Price] => 2.5E-5 
        [Total] => 0.0012475 
        [FillType] => PARTIAL_FILL 
        [OrderType] => SELL 
       ) 
      ... 

:

은 $ 배열의 내용을 인쇄하는 경우, 당신은이 같은 구조있어 볼 수 있습니다

<?php 
$json_string = file_get_contents("https://bittrex.com/api/v1/public/getmarkethistory?market=BTC-HYPER&count=5"); 

echo "<table>\n"; 
$array = json_decode($json_string); 
foreach ($array->result as $o) { 
    echo "<tr><td>$o->TimeStamp</td></tr>\n"; 
} 
echo "</table>\n"; 

출력 :

<table> 
<tr><td>2014-06-12T04:36:32.227</td></tr> 
<tr><td>2014-06-12T04:23:22.683</td></tr> 
<tr><td>2014-06-12T04:01:43.217</td></tr> 
<tr><td>2014-06-12T02:02:29.03</td></tr> 
... 
관련 문제