【问题标题】:Parse nested json in jquery and display in HTML page在 jquery 中解析嵌套的 json 并在 HTML 页面中显示
【发布时间】:2016-02-17 10:25:12
【问题描述】:

我需要解析下面的json并显示在一个html页面中。

  1. 在下拉列表中显示 COLA、COLB、COLC
  2. 在 html 表格中显示类型和索引的值。

JSON

{
  "mydb1": {
    "mappings": {
      "TAB1": {
        "properties": {
          "COLA": {
            "type": "string",
            "index": "not_analyzed"
          },
          "COLB": {
            "type": "string",
            "index": "not_analyzed"
          },
          "COLC": {
            "type": "string",
            "index": "not_analyzed"
          }
        }
      }
    }
  }
}

【问题讨论】:

  • 你试过研究吗?

标签: jquery json html parsing


【解决方案1】:

试试这个:

<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<div id="dropDown"></div>
<table id='tableVal' border='1'></table>
<script>
  $(document).ready(function() {
    var jsonStr = '{\
            "mydb1": {\
                "mappings": {\
                    "TAB1": {\
                        "properties": {\
                            "COLA": {\
                                "type": "string",\
                                "index": "not_analyzed"\
                            },\
                            "COLB": {\
                                "type": "string",\
                                "index": "not_analyzed"\
                            },\
                            "COLC": {\
                                "type": "string",\
                                "index": "not_analyzed"\
                            }\
                        }\
                    }\
                }\
            }\
        }';
    var jsonObj = JSON.parse(jsonStr);
    var drpDwn = '<select>',
      tabData = '';
    //console.log(jsonObj.mydb1.mappings.TAB1.properties);
    var temp = jsonObj.mydb1.mappings.TAB1.properties;
    $.each(temp, function(str, value) {
      drpDwn += '<option>' + str + '</option>';
      console.log(value.index);
      tabData += '<tr><td>' + value.type + '</td><td>' + value.index + '</td></tr>';
    });
    drpDwn += '</select>';
    $('#dropDown').html(drpDwn);
    $('#tableVal').html(tabData);
    //$.each(jsonObj.)
  });
</script>

【讨论】:

  • 非常有帮助。非常感谢 Sanjay Kumar N S
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-02
  • 2011-01-05
  • 2019-10-24
  • 1970-01-01
  • 1970-01-01
  • 2019-12-28
  • 2019-10-15
相关资源
最近更新 更多