json 파일 (리스트) 목록 뿌려주기

🙏SIR UI/UX 디자인 의뢰(~7/31)
json 파일 (리스트) 목록 뿌려주기

QA

json 파일 (리스트) 목록 뿌려주기

본문


{
    "status": "200",
    "api_mid": "1",
    "totalRecord": "3",
    "data": [
        {
            "no": 1,
            "test_key": "12",
            "test_name": "첫번째 항목",
            "test_company": "123",
            "test_thum_url": "https:\/\/test.com\/data\/2025\/02\/05\/1738748071_사과.jpg",
            "test_file_url": "https:\/\/test.com\/data\/2025\/02\/05\/1738748071_참외.jpg",
            "test_file_size": ""
        },
        {
            "no": 2,
            "test_key": "5",
            "test_name": "두번째 항목",
            "test_company": "123",
            "test_thum_url": "",
            "test_file_url": "",
            "test_file_size": ""
        },
        {
            "no": 3,
            "test_key": "10",
            "test_name": "세번째 항목 ",
            "test_company": "",
            "test_thum_url": "https:\/\/test.com\/data\/2025\/02\/05\/1738746852_수박.jpg",
            "test_file_url": "https:\/\/test.com\/data\/2025\/02\/05\/1738746852_딸기.jpg",
            "test_file_size": ""
        }
    ]
}

 

 

위와같이 json방식으로 뿌려주는 URL이 있을경우 어떻게 리스트(목록)을 보여지게 하는지요?

3개의 데이터를 리스트 형태로 보여주고싶습니다.

 

------------ URL을 읽어와서 JSON으로 뿌려지는것까지는 구현했습니다 ----

 

$url = "----제공된 URL ----";

$json_data = json_encode($data, true); 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$response  = curl_exec($ch);
$result = json_decode($response, true);
$result = iconv("EUC-KR", "UTF-8", $response);
print_r($response);

curl_close($ch);

 

이 질문에 댓글 쓰기 :

답변 2


<?php
$url = "----제공된 URL ----";
// cURL을 사용하여 데이터 가져오기
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
curl_close($ch);
// JSON 디코딩
$data = json_decode($response, true);
// JSON 데이터 검증
if ($data === null || !isset($data['data']) || !is_array($data['data'])) {
    die("데이터를 불러올 수 없습니다.");
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title>JSON 데이터 목록</title>
    <style>
        table { width: 100%; border-collapse: collapse; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f4f4f4; }
        img { max-width: 100px; height: auto; }
    </style>
</head>
<body>
<h2>JSON 데이터 목록</h2>
<table>
    <thead>
        <tr>
            <th>No</th>
            <th>Test Key</th>
            <th>Test Name</th>
            <th>Test Company</th>
            <th>Thumbnail</th>
            <th>File URL</th>
        </tr>
    </thead>
    <tbody>
        <?php if (!empty($data['data'])): ?>
            <?php foreach ($data['data'] as $item): ?>
                <tr>
                    <td><?= htmlspecialchars($item['no']) ?></td>
                    <td><?= htmlspecialchars($item['test_key']) ?></td>
                    <td><?= htmlspecialchars($item['test_name']) ?></td>
                    <td><?= htmlspecialchars($item['test_company']) ?></td>
                    <td>
                        <?= !empty($item['test_thum_url']) ? "<img src='".htmlspecialchars($item['test_thum_url'])."' alt='썸네일'>" : "없음" ?>
                    </td>
                    <td>
                        <?= !empty($item['test_file_url']) ? "<a href='".htmlspecialchars($item['test_file_url'])."' target='_blank'>파일 보기</a>" : "없음" ?>
                    </td>
                </tr>
            <?php endforeach; ?>
        <?php else: ?>
            <tr><td colspan="6">데이터가 없습니다.</td></tr>
        <?php endif; ?>
    </tbody>
</table>
</body>
</html>

이미 배열로 만드셨네요.

$response['data']안에 목록들의 행이 있기때문에

for, foreach등을 사용하여 루프돌리시면됩니다.

답변을 작성하시기 전에 로그인 해주세요.
전체 5

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT