2016-07-13 40 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); 
     }