2016-07-13 36 views
-1

調用API後,我得到了以下數據

$scope.twotap_builtin_cart = { 
     "sites":{ 
       "571fb46730bb1f373d00bdb4":{ 
         "info":{"logo":"https://core.twotap.com/system/sites/logos/571f/b467/30bb/1f37/3d00/bdb4/small/571fb46730bb1f373d00bdb4.png?1468389436927", 
         "name":"Overstock", 
         "url":"overstock.com" 
       },    
       "currency_format":"$AMOUNT", 
       "coupon_support":false, 
       "gift_card_support":false, 
       "checkout_support":["noauthCheckout"], 
       "shipping_countries_support":["United States of America"], 
       "billing_countries_support":["United States of America"], 
       "shipping_options":{"cheapest":"Default shipping option"}, 
       "returns":"<p><span style=\"font-size:16px\"><strong>Overstock Return Policy</strong></span></p>\r\n\r\n<p>You can initiate a return for most new and unopened items&nbsp;within 30 days of delivery for a full refund.* You must return items in new or unused condition with all original materials included with the shipment. We must receive your returned items at our processing facility within 45 days of delivery (see&nbsp;Product-Specific Return Policies&nbsp;for exceptions). We inspect all returned items and award a partial refund for opened or used products. If the return is a result of our error or defective product, we will refund the full cost of the merchandise and shipping charges.</p>\r\n\r\n<p>If you initiate a return after 30 days, we may issue a partial refund* depending on elapsed time and condition of merchandise.</p>\r\n\r\n<p><strong>Defective Product and Returns Due to Our Error</strong></p>\r\n\r\n<p>You may return defective items, items damaged at the time of receipt, and incorrect items received without penalty.** Your return shipping charges will be waived.</p>\r\n\r\n<p>When we receive your return, we inspect the item and provide a replacement or issue a refund. If the product is not defective, return shipping fees will apply and/or a partial refund may be issued.</p>\r\n\r\n<p><strong>Products Returned After 30 days</strong></p>\r\n\r\n<ul>\r\n\t<li>Because products devalue over time, we offer partial refunds for items returned over 30 days after delivery</li>\r\n\t<li>We may reduce the refund based on the condition of the merchandise</li>\r\n\t<li>We may also deduct the original and return shipping charges from your refund</li>\r\n\t<li>If we send you a replacement item, we must receive the original item at our warehouse within 30 days your return initiation. Otherwise, we will charge you full price for the replacement item</li>\r\n</ul>\r\n\r\n<p><em>For more about return policy rules and exceptions you can visit :&nbsp;https://help.overstock.com/app/answers/detail/a_id/1</em></p>\r\n", 
       "add_to_cart":{"a6eda98b2e60363a67efb985ef622cea":{"original_url":"http://www.jdoqocy.com/click-3726866-10654383?url=http%3A%2F%2Fwww.overstock.com%2F7281112%2Fproduct.html%3FTRACK%3Daffcjfeed%26CID%3D207442%26fp%3DF","clean_url":"http://www.overstock.com/7281112/product.html?CID=207442", 
       "weight":"1000", 
       "status":"done", 
       "required_fields":{ 
        "quantity":{ 
          "data":[{"input_type":"text","input_name":"INPUT"}]}}, 
          "discounted_price":null, 
          "original_price":null, 
          "pickup_support":false, 
          "url":"http://www.overstock.com/7281112/product.html?TRACK=affcjfeed&CID=207442&fp=F","required_field_values":{},"required_field_names":["quantity"],"categories":["Sports & Toys","Toys & Hobbies","Games & Puzzles","Board Games"],"description":"<p>ITEM#: 14756475</p> From a different age when people had time to sit and play games together, the Britannia Games Compendium is packed full of fun. Based on a product from the 1940s the Britannia Games Compendium has something for all ages.\n<br><br>\n<ul>\n<li>Brand: Perisphere and Trylon</li>\n<li>The Britannia Compendium of Games</li>\n<li>Model: PTRG-1088</li>\n<li>Based on a product from the 1940s the Britannia Games Compendium has something for all ages</li> <li>Beautifully made and highly collectable</li>\n<li>Number of players: 1 or more</li>\n<li>Includes: Chess, Draughts, Dominoes, Pick-up Sticks, Snap, Tiddlywinks, Playing Cards, Muggins, Poker Dice, Horse Racing Game, one (1) multiple fold-out game board, and instructions</li>\n<li>Recommended for ages 5 years and older</li>\n</ul>","alt_images":["http://ak1.ostkcdn.com/images/products/7281112/Perisphere-and-Trylon-The-Britannia-Compendium-of-Games-f17b48f6-8c81-4a0e-af12-fa2e3d97ac25_320.jpg"],"image":"http://ak1.ostkcdn.com/images/products/7281112/Perisphere-and-Trylon-The-Britannia-Compendium-of-Games-f17b48f6-8c81-4a0e-af12-fa2e3d97ac25_600.jpg","price":"$37.49","title":"Perisphere and Trylon The Britannia Compendium of Games"}}}},"user_id":null,"unknown_urls":[],"cart_id":"5785d83c493bfb7f2a352cde","notes":null,"country":"us","stored_field_values":{},"message":"done","description":"AddToCart has been completed.","product_urls":[]} 

然後我保存的cookies內這個數據。

$cookies['xxx'] = $scope.twotap_builtin_cart; 

現在,當我試圖從cookie中檢索這些數據時,它顯示我爲Object。我試圖在控制檯內打印它顯示的對象Object

var t = $cookies['xxx']; 
console.log(t); 
$scope.recentItemsx = angular.toJson(t); 
console.log($scope.recentItemsx); 
console.log(JSON.parse($cookies['xxx'])); 

我試過JSON.parse,anglar.toJson和jsonstringify。沒有什麼能解決這個

我用cookie存儲試圖

$cookieStore.put('recentx',$scope.twotap_builtin_cart); 

我也得到了以下控制檯的警告曲奇

「recentx」可能未設置或溢出,因爲它太大了(6675> 4096個字節)! 我打印的另一個控制器

console.log($cookieStore.get('recentx')); 

它給我輸出[對象對象]。我無法用這個工作。我需要用於ng-repeat的json數據 我不能把對象或獲取對象,因爲那些角度版本大於1.4

+1

請分享小提琴,所以我們可以重現和幫助你 – Saqueib

回答

0

嘗試使用JSON.stringify(t)將其轉換爲字符串,然後JSON.parse( t)把它變成JSON。

或者嘗試angular.toJson(t)

+0

變種J = JSON.stringify(T); console.log(JSON.parse(j)); 打印的[object Object] – Rajib

0

首先要cookies內

var x= angular.toJson($scope.twotap_builtin_cart); 
$cookies['xxx']= x; 

保存到從餅乾

var t = $cookies['xxx']; 
var pota = angular.fromJson(t); 
console.log(pota); 

應該對所有類似的格式JSON數據檢索工作。但對我而言,數據太大了。所以我不能保存餅乾。現在我正在使用localstorage。首先使用$ window服務並在控制器內注入$ window。 要設置

var ret= angular.toJson($scope.twotap_builtin_cart); 
    $window.localStorage.setItem('recent',ret); 

要檢索

if(angular.isDefined($window.localStorage.getItem('recent'))); 
     { 
     var tem =$window.localStorage.getItem('recent'); 
     var pota = angular.fromJson(tem); 
     console.log(pota); 
     }