2016-01-06 101 views
0

我有一個問題,如何更改json對象內的json對象的隨機數對象。讓我用我的代碼和評論進一步解釋這一點。將JSON字符串轉換爲JSON對象的隨機數內的對象

我廠可提供JSON對象

//Factory for products 
app.factory('productsFactory', ['$http', '$location', function($http, $location){ 
    var factory = {}; 
    factory.getlatestProductsList = function(n){ 
     return $http.get($location.protocol() + '://' + $location.host() + '/server/api/products/latest/'+n); 
    } 
    return factory; 
}]); 

這個工廠返回對象的一些隨機數

0: Object 
active: "1" 
alias: "baumbach-circle" 
date_c: "2016-01-06 08:09:54" 
date_u: null 
description: "Corrupti fugit iste quo sunt quidem voluptatibus dolorem. Eos velit architecto veritatis doloribus. Corporis sequi cupiditate possimus voluptates ut consequatur. Accusantium libero qui est sunt et." 
id_category: "46" 
id_product: "25" 
id_user: "177" 
images: "[{"thumbImage":"http:\/\/lorempixel.com\/250\/150\/?63763","image":"http:\/\/lorempixel.com\/1024\/768\/?52630","position":0},{"thumbImage":"http:\/\/lorempixel.com\/250\/150\/?99795","image":"http:\/\/lorempixel.com\/1024\/768\/?84669","position":1},{"thumbImage":"http:\/\/lorempixel.com\/250\/150\/?17506","image":"http:\/\/lorempixel.com\/1024\/768\/?88926","position":2},{"thumbImage":"http:\/\/lorempixel.com\/250\/150\/?73869","image":"http:\/\/lorempixel.com\/1024\/768\/?91917","position":3},{"thumbImage":"http:\/\/lorempixel.com\/250\/150\/?70019","image":"http:\/\/lorempixel.com\/1024\/768\/?18509","position":4}]" 
name: "Baumbach Circle" 
__proto__: Object 
1: Object 
active: "1" 
alias: "elta-road" 
date_c: "2016-01-06 08:09:53" 
date_u: null 
description: "Culpa perferendis dolores rerum deleniti vero cumque. Similique explicabo beatae est quo sit nisi. Et a voluptatem nihil in. Voluptates modi qui est ducimus corrupti." 
id_category: "46" 
id_product: "24" 
id_user: "73" 
images: "[{"thumbImage":"http:\/\/lorempixel.com\/250\/150\/?53746","image":"http:\/\/lorempixel.com\/1024\/768\/?49502","position":0},{"thumbImage":"http:\/\/lorempixel.com\/250\/150\/?75052","image":"http:\/\/lorempixel.com\/1024\/768\/?77727","position":1},{"thumbImage":"http:\/\/lorempixel.com\/250\/150\/?32463","image":"http:\/\/lorempixel.com\/1024\/768\/?76121","position":2},{"thumbImage":"http:\/\/lorempixel.com\/250\/150\/?61377","image":"http:\/\/lorempixel.com\/1024\/768\/?89434","position":3},{"thumbImage":"http:\/\/lorempixel.com\/250\/150\/?86873","image":"http:\/\/lorempixel.com\/1024\/768\/?82513","position":4}]" 
name: "Elta Road" 
__proto__: Object 

的問題是圖像JSON是字符串格式,所以我必須使用JSON .parse(至少我認爲是這樣,因爲我是新手)將它們轉換爲JSON

我的問題是:是否有一些簡單的方法可以將所有圖像數據解析爲json或者我必須使用forEach來做到這一點,如果是這樣,代碼將如何。我不想在工廠內實現這種效果,所以我不必在每個調用這個工廠的控制器中重複代碼。

如果您需要任何其他信息,請讓我知道,我會提供。先謝謝你。

+0

「*代碼如何看起來像*」 - 你卡在什麼?這聽起來像你知道如何使用JSON.parse,以及如何使用forEach;你大多聽起來像是你所有的設定,因爲它不應該是複雜的。我並不是指責你懶惰發佈這個問題,我只是真的不確定你需要什麼幫助。 編輯:劃痕,忘記異步/然後部分,這可以肯定絆倒某人的想法。 – Katana314

回答

1

你可以返回它

app.factory('productsFactory', ['$http', '$location', function($http, $location){ 
    var factory = {}; 
    factory.getlatestProductsList = function(n){ 
     var url = $location.protocol() + '://' + $location.host() + '/server/api/products/latest/'+n; 
     return $http({ 
      method: 'GET', 
      url: url 
     }).then(function successCallback(response) { 
      // you can edit the response her, and parse it 
      return response ; 
     }, function errorCallback(response) { 

     }); 
    } 
    return factory; 
}]); 
+0

是的,這是我正在考慮如何去做的方式,但我希望得到一些更流暢的解決方案,而不是爲每一個詭計寫出整體。所以基本上我正在尋找這些編輯響應行 –

0

您可以使用$ http返回promise的事實,然後在返回promise之前對數據進行一些處理。這是否對你的工作 - 讓我知道你是否在ES5

factory.getlatestProductsList = function(n){ 
    return $http.get($location.protocol() + '://' + $location.host() + '/server/api/products/latest/'+n) 
    .then(data => data.map(elem => { 
     var tmp = Json.parse(elem.images); 
     elem.images = tmp 
     return elem; 
    })); 

需要ES5

factory.getlatestProductsList = function(n){ 
    return $http.get($location.protocol() + '://' + $location.host() + '/server/api/products/latest/'+n) 
    .then(function(data) { 
     return data.map(function(elem) { 
     var tmp = Json.parse(elem.images); 
     elem.images = tmp 
     return elem; 
     }); 
    }); 
+0

看起來不錯,但我認爲他要求在工廠內部而不是每次打電話。鑑於這也只是修改,而不是轉換,你可以用forEach而不是map來完成。 – Katana314

+0

我從來沒有在javascript中看到使用'=>'。這怎麼叫? – AlainIb

+0

這是一個ES2015'胖箭頭',是一個匿名函數調用的簡寫 –

0

之前編輯由$ HTTP返回的答案粘貼代碼,如果有人想看看它是怎麼樣的

//Factory for products 
app.factory('productsFactory', ['$http', '$location', function($http, $location){ 
    var factory = {}; 
    factory.getlatestProductsList = function(n){ 
     var url = $location.protocol() + '://' + $location.host() + '/server/api/products/latest/'+n; 
     return $http({ 
      method: 'GET', 
      url: url 
     }).then(function successCallback(response) { 
      var pros = response.data; 
      pros.forEach(function(item){ 
       var pictures = JSON.parse(item.images); 
       delete item.images; 
       item.images = pictures; 
      }) 
      return pros; 
     },function errorCallback(response) { 
      console.log('error fetching latest products: ' + response); 
     }); 
    } 
    return factory; 
}]);