2015-12-01 85 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 –