테스트 사이트 - 개발 중인 베타 버전입니다

json 파일 (리스트) 목록 뿌려주기 채택완료

수난이대 9개월 전 조회 1,384

</p>

<p>{

    "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": ""

        }

    ]

}</p>

<p>

 

 

위와같이 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개

채택된 답변
+20 포인트
glitter0gim
9개월 전

</p>

<p><?php

$url = "----제공된 URL ----";</p>

<p>// cURL을 사용하여 데이터 가져오기

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response  = curl_exec($ch);

curl_close($ch);</p>

<p>// JSON 디코딩

$data = json_decode($response, true);</p>

<p>// JSON 데이터 검증

if ($data === null || !isset($data['data']) || !is_array($data['data'])) {

    die("데이터를 불러올 수 없습니다.");

}</p>

<p>?></p>

<p><!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></p>

<p><h2>JSON 데이터 목록</h2></p>

<p><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></p>

<p></body>

</html></p>

<p>

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

수난이대
9개월 전
감사합니다.
이렇게 긴 코딩까지 직접 해주실줄은...
정말 감사합니다.

댓글을 작성하려면 로그인이 필요합니다.

그누위즈
9개월 전

이미 배열로 만드셨네요.

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

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

로그인 후 평가할 수 있습니다

답변에 대한 댓글 2개

수난이대
9개월 전
답변 감사합니다.
g
glitter0gim
9개월 전
`/_

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인