반응형

웹서버로 만든 웹페이지에서 JSON 데이터를 분석하고 테이블이나 리스트등 원하는 형식으로 보여 줄 수 있다.


2019/10/08 - [Linux] - Build your own web server with Fedora linux - 페도라 리눅스로 간단한 웹서버 만들기


웹페이지에 보여 줄 데이터를 준비한다.


데이터를 JSON 형식으로 변환 한다. 아래 링크에서 간단히 변환 할 수 있다.

https://shancarter.github.io/mr-data-converter/


변환된 데이터는 data.json 파일로 저장 한다.


html/data 디렉토리를 만들고 data.json 파일을 저장 한다.



아래 파일과 같은 내용으로 index.php를 만들고 html 디렉토리에 저장 한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
    $file_contents = file_get_contents('./data/data.json');
    $data = json_decode($file_contents, true);
?>
 
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>PHP JSON parser sample</title>
    </head>
   
    <body>
    <table border="1" bordercolor="blue">
 
    <tr bgcolor="yellow">
    <th> 날짜 </th>
    <th> 아이템 </th>
    <th> 가격 </th>
    <th> 수량 </th>
    <th> 합계 </th>
    </tr>
 
    <?php foreach ($data as $row) { ?>
        <tr align="center">
 
        <td> <?php print $row['date']; ?> </td>
        
        <td> <?php print $row['item']; ?> </td>
        
        <td> <?php print $row['price']; ?> </td>
        
        <td> <?php print $row['quantity']; ?> </td>
        
        <td> <?php print $row['total']; ?> </td>
 
        </tr>
        
    <?php } ?>
 
    </table>
 
 
    <?php
    foreach ($data as $row) {
        print $row['date'];
        print ' , ';
        print $row['item'];
        print ' , ';
        print $row['price'];
        print ' , ';
        print $row['quantity'];
        print ' , ';
        print $row['total'];
        print '<br />';
    }
    ?>
 
 
    <?php
    foreach ($data as $row) {
        printf("<li>%s %s %s %s %s</li>\n", $row['date'], $row['item'], $row['price'], $row['quantity'], $row['total']);
    }
    ?>
    
    </body>
 
</html>



서버에 접속하면 data.json의 내용이 index.php에 설정한대로 표시된다.


반응형
Posted by J-sean
:
반응형

서버에서 데이터를 받아 오는 경우 모든 문자가 알파벳이나 숫자라면 상관 없지만 한글이 섞여 있다면 글자가 깨지는 문제가 발생하고 어떤 인코딩이 사용되었는지 확인하기 어려운 경우가 있다. 아래와 같이 단순한 방법으로 간단히 확인 할 수 있다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
try {
    Log.d("Encoding Check""utf-8 -> euc-kr: " + new String(str.getBytes("utf-8"), "euc-kr"));
    Log.d("Encoding Check""utf-8 -> ksc5601: " + new String(str.getBytes("utf-8"), "ksc5601"));
    Log.d("Encoding Check""utf-8 -> x-windows-949: " + new String(str.getBytes("utf-8"), "x-windows-949"));
    Log.d("Encoding Check""utf-8 -> iso-8859-1: " + new String(str.getBytes("utf-8"), "iso-8859-1"));
 
    Log.d("Encoding Check""iso-8859-1 -> euc-kr: " + new String(str.getBytes("iso-8859-1"), "euc-kr"));
    Log.d("Encoding Check""iso-8859-1 -> ksc5601: " + new String(str.getBytes("iso-8859-1"), "ksc5601"));
    Log.d("Encoding Check""iso-8859-1 -> x-windows-949: " + new String(str.getBytes("iso-8859-1"), "x-windows-949"));
    Log.d("Encoding Check""iso-8859-1 -> utf-8: " + new String(str.getBytes("iso-8859-1"), "utf-8"));
 
    Log.d("Encoding Check""euc-kr -> utf-8: " + new String(str.getBytes("euc-kr"), "utf-8"));
    Log.d("Encoding Check""euc-kr -> ksc5601: " + new String(str.getBytes("euc-kr"), "ksc5601"));
    Log.d("Encoding Check""euc-kr -> x-windows-949: " + new String(str.getBytes("euc-kr"), "x-windows-949"));
    Log.d("Encoding Check""euc-kr -> iso-8859-1: " + new String(str.getBytes("euc-kr"), "iso-8859-1"));
 
    Log.d("Encoding Check""ksc5601 -> euc-kr: " + new String(str.getBytes("ksc5601"), "euc-kr"));
    Log.d("Encoding Check""ksc5601 -> utf-8: " + new String(str.getBytes("ksc5601"), "utf-8"));
    Log.d("Encoding Check""ksc5601 -> x-windows-949: " + new String(str.getBytes("ksc5601"), "x-windows-949"));
    Log.d("Encoding Check""ksc5601 -> iso-8859-1: " + new String(str.getBytes("ksc5601"), "iso-8859-1"));
 
    Log.d("Encoding Check""x-windows-949 -> euc-kr: " + new String(str.getBytes("x-windows-949"), "euc-kr"));
    Log.d("Encoding Check""x-windows-949 -> utf-8: " + new String(str.getBytes("x-windows-949"), "utf-8"));
    Log.d("Encoding Check""x-windows-949 -> ksc5601: " + new String(str.getBytes("x-windows-949"), "ksc5601"));
    Log.d("Encoding Check""x-windows-949 -> iso-8859-1: " + new String(str.getBytes("x-windows-949"), "iso-8859-1"));
catch(Exception e) {
    e.printStackTrace();
}



Logcat을 확인해 보면 'iso-8859-1 -> utf-8'이 정상적으로 한글을 표시했다.


그런데 위 코드 중 'utf-8 -> iso-8859-1'은 Logcat에 출력 되지 않는다. System.out.println()으로 해도 출력되지 않지만, 이 문자열을 기기에서 출력해 보면 깨진 한글로 표시는 된다. 이유는 알 수 없다.

1
    Log.d("Encoding Check""utf-8 -> iso-8859-1: " + new String(str.getBytes("utf-8"), "iso-8859-1"));



반응형
Posted by J-sean
: