2014-10-25 5 views
-3

나는 아래와 같은 json 응답을 얻고있다, 나는 명확한 형식으로 PHP를 사용하여 표시하고자합니다.json 데이터 표시 형식을 사용하여 PHP를

JSON 응답 :

{"id":"1001","subject":"English","month":"October","group":{"native":"60","others":"40"},"status":Yes}" 

I는 다음과 같이 표시 할 :

<table border="1"> 
<tr> 
<td>Id</td><td>Subject</td><td>Month</td> 
</tr> 
<tr><td>10001</td><td>English</td><td>October</td></tr> 
<tr> 
<td>Group:</td><tr><td>Native</td><td>60</td><td>Others</td><td>40</td></tr> 
</tr> 
<tr> 
<td>Status</td><td>Yes</td> 
</tr> 
</table> 
+2

를 중지이야? 'json_decode()'를 호출하고, 루프를 돌린다. – Barmar

+2

SO는 무료 코딩 서비스가 아니므로 직접 작업하십시오. 작동시키지 못하면 시도한 것을 게시하고 해결하도록 도와 드리겠습니다. – Barmar

답변

0

json_decode()를 사용하여 json 인코딩 된 문자열을 PHP 변수로 변환 할 수 있습니다.

이 함수는 assoc 플래그를 true로 설정하면 예제 json을 StdObject 또는 간단한 배열로 반환합니다.

당신이 단순히 해당 키를 사용하여 HTML 테이블에 값을 삽입 할 수 있습니다 일단 :

$json='{"id":"1001","subject":"English","month":"October","group":{"native":"60","others":"40"},"status":Yes}'; 
$phpArray=json_decode($json,true) 

HTML보기 :

<tr><td><?php echo $phpArray['id']?></td><td><?php echo $phpArray['subject']?></td><td><?php echo $phpArray['month']?></td></tr> 

참조 : 당신을 http://php.net/manual/en/function.json-decode.php

+0

그것의 일. 감사합니다. – user3164590

0

사용

$jsonObject = json_decode($jsonResponse); 

을 그리고 당신이 얻을 :

object(stdClass)#1 (5) { ["id"]=> int(1001) ["subject"]=> string(7) "English" ["month"]=> string(7) "October" ["group"]=> object(stdClass)#2 (2) { ["native"]=> int(60) ["others"]=> int(40) } ["status"]=> string(3) "Yes" } 

다음 예 : echo $jsonObject->subject;

관련 문제