【问题标题】:Parse Json Data into HTML without having Json array name在没有 Json 数组名称的情况下将 Json 数据解析为 HTML
【发布时间】:2015-12-01 08:43:31
【问题描述】:

我有一个 web 服务返回 json 数据列表。但数据没有数组名称。 数据的JSON格式如下图:

[{"itemno":1256,"offerPercent":10,"bulkDiscount":20,"regQtyBuyLimit":10,"offerQtyBuyLimit":5,"minReOrderLevel":2,"pkg":"5kg","addedOn":"2015-10-11","updatedOn":"2015-10-12","mrp":500,"regPrice":400,"minBulkQty":50}]

这是来自 mysql 通过 web 服务调用。

我想把它解析成html表格。

我的问题是:如何解析没有arrayname的数据或者如何定义数组的名称然后解析它?

【问题讨论】:

  • 你指的是什么数组名?您粘贴的 JSON 数据是非法的,请注意最后一个“,”。如果您使用var jsonData = JSON.parse('[{"itemno":1256,"offerPercent":10,"bulkDiscount":20,"regQtyBuyLimit":10}]') 删除它,则会将您的JSON 字符串解析为object 类型,您可以将其寻址为jsonData[0]
  • @kayess 我已经编辑了 json 数据; "," 输入错误。
  • @kayess 数组名表示开始时 json 数据上显示的名称。

标签: html json swing parsing model-view-controller


【解决方案1】:

在 responseData 上成功调用 $.parseJSON(responseData) 时从服务器获取响应(如果您使用简单的 http 表单提交请求或 ajax 调用)。

使用警报来验证您的数据。

【讨论】:

  • 我不知道如何解析没有数组名的 json 数据......请帮助我......
  • 你有数据就用这个函数
【解决方案2】:

我再次自己解决了我的问题。 这次我会告诉你怎么做。

这里是jsp文件的完整代码:-

<html>
<head>
<title>Lets See</title>
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
 $(document).ready(function(){
           $.getJSON('http://localhost:8080/OnlineStore/kmsg/grocery/item',
                function (json) {
                    var tr;
                    for (var i = 0; i < json.length; i++) {
                        tr = $('<tr/>');
                        tr.append("<td>" + json[i].itemno + "</td>");
                        tr.append("<td>" + json[i].offerPercent + "</td>");
                        tr.append("<td>" + json[i].bulkDiscount + "</td>");
                        tr.append("<td>" + json[i].regQtyBuyLimit + "</td>");
                        tr.append("<td>" + json[i].offerQtyBuyLimit + "</td>");
                        tr.append("<td>" + json[i].minReorderLevel + "</td>");
                        tr.append("<td>" + json[i].pkg + "</td>");
                        tr.append("<td>" + json[i].addedOn + "</td>");
                        tr.append("<td>" + json[i].updatedOn + "</td>");
                        tr.append("<td>" + json[i].mrp + "</td>");
                        tr.append("<td>" + json[i].regPrice + "</td>");
                        tr.append("<td>" + json[i].minBulkqty + "</td>");
                        $('table').append(tr);
                    }                   
                });
            });   
        </script>
</head>
<body>
    <table border="1">
        <tr>
            <th>ItemNo</th>
            <th>OfferPercent</th>
            <th>BulkDiscount</th>
            <th>regQtyBuyLimit</th>
            <th>offerQtyBuyLimit</th>
            <th>minReorderLevel</th>
            <th>pkg</th>
            <th>addedOn</th>
            <th>updatedOn</th>
            <th>mrp</th>
            <th>regPrice</th>
            <th>minBulkqty</th>
        </tr>
    </table>
    <button>Get Item</button>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2018-06-02
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 2014-12-01
    • 2020-03-28
    • 1970-01-01
    • 2017-06-10
    相关资源
    最近更新 更多