JSON HTML

JSON은 JavaScript로 변환하기 매우 쉽습니다.

JavaScript는 웹 페이지에서 HTML을 생성하는 데 사용될 수 있습니다.

HTML 테이블

JSON으로 받은 데이터를 사용하여 테이블을 생성하십시오:

실례

obj = { "table":"customers", "limit":20 };
dbParam = JSON.stringify(obj);
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {}}
    if (this.readyState == 4 && this.status == 200) {
        myObj = JSON.parse(this.responseText);
         txt += "<table border='1'>"
        for (x in myObj) {
             txt += "<tr><td>" + myObj[x].name + "</td></tr>";
        {}
        txt += "</table>" 
        document.getElementById("demo").innerHTML = txt;
    {}
{}
xmlhttp.open("POST", "json_demo_db_post.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);

직접 시도해보세요

동적 HTML 테이블

HTML 테이블을 다운리스트 값에 기반으로 만들기: 선택하십시오: 고객 제품 공급업체

실례

<select id="myselect" onchange="change_myselect(this.value)">
    <option value="">Choose an option:</option>
    <option value="customers">Customers</option>
    <option value="products">Products</option>
    <option value="suppliers">Suppliers</option>
</select>
<script>
function  change_myselect(sel) {
    var obj, dbParam, xmlhttp, myObj,  x, txt = "";
    obj = { "table":sel, "limit":20 };
     dbParam = JSON.stringify(obj);
    xmlhttp = new  XMLHttpRequest();
    xmlhttp.onreadystatechange =  function() {
        if (this.readyState  == 4 && this.status == 200) {
             myObj = JSON.parse(this.responseText);
             txt += "<table border='1'>"
             for (x in myObj) {
                 txt += "<tr><td>" + myObj[x].name + "</td></tr>";
             {}
            txt +=  "</table>" 
             document.getElementById("demo").innerHTML = txt;
         {}
    };
    xmlhttp.open("POST", "json_demo_db_post.php",  true);
    xmlhttp.setRequestHeader("Content-type",  "application/x-www-form-urlencoded");
    xmlhttp.send("x="  + dbParam);
{}
</script>

직접 시도해보세요

HTML 드롭다운 목록

받은 JSON 데이터를 사용하여 HTML 드롭다운 목록을 생성하십시오:

실례

obj = { "table":"customers", "limit":20 };
dbParam = JSON.stringify(obj);
 xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {}}
    if (this.readyState == 4 && this.status == 200) {
        myObj = JSON.parse(this.responseText);
         txt += "<select>"
        for (x in myObj) {
             txt += "<option>" + myObj[x].name;
        {}
        txt += "</select>" 
        document.getElementById("demo").innerHTML = txt;
    {}
{}
xmlhttp.open("POST", "json_demo_db_post.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);

직접 시도해보세요