2015-12-01 55 views
0

我有一个Web服务返回json数据列表。但数据没有数组名称。数据 JSON格式如下所示:将Json数据解析为无Json数组名称的HTML

[{"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}] 

这是从通过Web服务调用MySQL的未来。

我想解析成html表格。

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

+0

你指的是什么样的数组名?您粘贴的JSON数据是非法的,请注意最后一个“,”。如果你使用'var jsonData = JSON.parse('[{“itemno”:1256,“offerPercent”:10,“bulkDiscount”:20,“regQtyBuyLimit”:10}]')'将你的JSON字符串解析为类型的'object',你可以将它定位为'jsonData [0]'。 – kayess

+0

@ kayess我编辑了json数据; “,”是错误的输入。 – RishiPandey

+0

@kayess数组名称表示在开始时显示在json数据上的名称。 – RishiPandey

回答

0

我再次自己解决了我的问题。 这次我会告诉你如何。

下面是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> 
0

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

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

+0

我不知道如何解析一个JSON数据没有数组名称......请帮我对此... – RishiPandey

+0

你有数据然后只是使用这个功能 –

+0

看到这个链接http://www.w3schools。 COM/JSON/json_eval.asp –