我如何从JSON中提取这些数据并将其放在表格中

时间:2016-02-06 05:36:37

标签: javascript jquery json

我想使用此代码,并从中提取数据并将其添加到表中。 我有一个JSFiddle链接,将其添加到表中的代码不起作用。不确定,但它与JSON数组有关吗?

http://jsfiddle.net/T7eQg/213/

        $(function(){

        var jsonObj = $.parseJSON('{"deals":{"items":[{"title":"Stripe Cashmere Blend Jumper \u00c2\u00a36 at George (Free C&C)","deal_link":"http:\/\/www.hotukdeals.com\/deals\/stripe-cashmere-blend-jumper-6-george-free-c-c-2390139?aui=1047","mobile_deal_link":"http:\/\/www.hotukdeals.com\/deals\/stripe-cashmere-blend-jumper-6-george-free-c-c-2390139?aui=1047","deal_image":"http:\/\/static.hotukdeals.com\/images\/threads\/2390139_1.jpg","description":"Update your winter essentials with this gorgeous stripe cashmere jumper. This lovely jumper designed with contrast stripe sleeves, will be an awesome update to your weekly rotation.\n\nbb8 ;)","submit_time":"7 hours, 29 minutes ago","hot_time":"3 hours, 3 minutes ago","poster_name":"bb8","temperature":129.08,"price":"6.00","timestamp":1454709758,"expired":"false","forum":{"name":"Deals","url_name":"deals"},"category":{"name":"Fashion","url_name":"fashion"},"merchant":{"name":"George (Asda George)","url_name":"direct.asda.com-1"},"tags":{"items":[]},"deal_image_highres":"http:\/\/static.hotukdeals.com\/images\/threads\/high-res\/2390139_1.jpg","deal_image_highres_width":372,"deal_image_highres_height":500},{"title":"12 pack polo mint for \u00c2\u00a31 Instore @ FarmFoods","deal_link":"http:\/\/www.hotukdeals.com\/deals\/12-pack-polo-mint-for-1-instore-farmfoods-2389584?aui=1047","mobile_deal_link":"http:\/\/www.hotukdeals.com\/deals\/12-pack-polo-mint-for-1-instore-farmfoods-2389584?aui=1047","deal_image":"http:\/\/static.hotukdeals.com\/images\/threads\/2389584_1.jpg","description":"12 pack polo mint for \u00c2\u00a31 Instore ","submit_time":"20 hours, 30 minutes ago","hot_time":"3 hours, 21 minutes ago","poster_name":"bulktrans","temperature":117,"price":"1.00","timestamp":1454662886,"expired":"false","forum":{"name":"Deals","url_name":"deals"},"category":{"name":"Groceries","url_name":"groceries"},"merchant":{"name":"FarmFoods","url_name":"farmfoods.co.uk"},"tags":{"items":[{"name":"Farmfoods"},{"name":"Polo Shirt"}]},"deal_image_highres":"http:\/\/static.hotukdeals.com\/images\/threads\/high-res\/2389584_1.jpg","deal_image_highres_width":500,"deal_image_highres_height":375}]},"total_results":1000}');
        var html = '<table border="1">';
        $.each(jsonObj.deals.items, function(key, value){
            html += '<tr>';
            html += '<td>' + value.title + '</td>';
    html += '<td>' + value.description + '</td>';
    html += '<td>' + value.price + '</td>';
            html += '</tr>';
        });
        html += '</table>';

        $('div').html(html);
    });

1 个答案:

答案 0 :(得分:0)

问题:

JSON中打破解析的东西是......

... JSON数据中的两个\n字符!

试试这个:http://jsfiddle.net/T7eQg/219/

说明:

\n在运行时由Javascript解析,导致JSON数据中的实际换行符。这意味着它不再是有效的代码并导致浏览器错误。如果您使用\\n代替,则可以轻松避免此问题,因为这会将反斜杠转义为正常的反斜杠而不是\n的一部分。

下次解析时,\n将再次形成并进行预期的换行。

其他信息:

  • \r\n相同(因为它会导致换行并且应该转义)
  • 以下是代码中两个 \n的位置: ... to your weekly rotation.\n\nbb8 ;)"...