2014-10-04 40 views
-1
{"result":[[{"Image":{"__type":"File","name":"tfss-772aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg","url":"http://f 
iles.parsetfss.com/49a11e0f-0c84-417e-b81b-58f5e97abae9/tfss-772aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg"},"Tit 
le":"The Expendables 3","featured_actors":" Sylvester Stallone, Jason Statham, Jet Li","genre":"Action, Adventure, Thril 
ler","imdb_rating":6.3,"plot_summary":"Barney augments his team with new blood for a personal battle: to take down Conra 
d Stonebanks, the Expendables co-founder and notorious arms trader who is hell bent on wiping out Barney and every singl 
e one of his associates.","running_time_mins":126,"trailer_video_link":"http://www.imdb.com/rg/VIDEO_PLAY/LINK//video/im 
db/vi1896197145/","viewer_rating":"18","objectId":"8cTkg769gM","createdAt":"2014-08-16T21:32:28.318Z","updatedAt":"2014- 
08-16T21:35:56.217Z","__type":"Object","className":"Movie"}],[{"Image":{"__type":"File","name":"tfss-65707002-e78b-47bd- 
9c7a-da072f388e25-deliver.jpg","url":"http://files.parsetfss.com/49a11e0f-0c84-417e-b81b-58f5e97abae9/tfss-65707002-e78b 
-47bd-9c7a-da072f388e25-deliver.jpg"},"Title":"Deliver Us From Evil","featured_actors":"Eric Bana, Édgar Ramírez, Oliv 
ia Munn","genre":"Crime, Horror, Thriller","imdb_rating":6.5,"plot_summary":"New York police officer Ralph Sarchie inves 
tigates a series of crimes. He joins forces with an unconventional priest, schooled in the rites of exorcism, to combat 
the possessions that are terrorizing their city.","running_time_mins":118,"trailer_video_link":"http://www.imdb.com/vide 
o/imdb/vi1746054425/?ref_=tt_ov_vi","viewer_rating":"15","objectId":"BoCM1zbOvh","createdAt":"2014-08-25T14:27:24.331Z", 
"updatedAt":"2014-08-25T19:49:09.910Z","__type":"Object","className":"Movie"},{"Image":{"__type":"File","name":"tfss-772 
aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg","url":"http://files.parsetfss.com/49a11e0f-0c84-417e-b81b-58f5e97abae 
9/tfss-772aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg"},"Title":"The Expendables 3","featured_actors":" Sylvester 
Stallone, Jason Statham, Jet Li","genre":"Action, Adventure, Thriller","imdb_rating":6.3,"plot_summary":"Barney augments 
his team with new blood for a personal battle: to take down Conrad Stonebanks, the Expendables co-founder and notorious 
arms trader who is hell bent on wiping out Barney and every single one of his associates.","running_time_mins":126,"tra 
iler_video_link":"http://www.imdb.com/rg/VIDEO_PLAY/LINK//video/imdb/vi1896197145/","viewer_rating":"18","objectId":"8cT 
kg769gM","createdAt":"2014-08-16T21:32:28.318Z","updatedAt":"2014-08-16T21:35:56.217Z","__type":"Object","className":"Mo 
vie"}]]} 

這是使用curl的JSON響應。我試圖從數組中檢索唯一的對象。如您所見,它最終返回了第一個對象的副本。我已經嘗試了很多解決方案,即使是Underscore.js _.uniq方法。我仍然無法達到正確的結果。我將不勝感激任何幫助。提前致謝。從json獲取唯一對象的數組javascript

這是我實現

Parse.Promise.when(promises).then(
    function() { 
     var result = []; 
     _.each(arguments, function(object) { 
      result.push(object); // each object is an array of movieIds 
     }); 

     var movies = _.flatten(result); 
     var uniqueMovies = _.uniq(movies, function(movie){ 
              return movie.Title; 
     }); 

     response.success(uniqueMovies); 
    }, 
    function(error) { 
     response.error(error); 
    } 
); 
/// 
}); 
+0

你有什麼特別的嘗試?具體結果是什麼?我還建議您格式化數據,以便它是人類可讀的。 – 2014-10-04 21:13:04

+0

請格式化您的代碼以使其可讀。它需要縮進和換行符。 – jfriend00 2014-10-04 21:34:28

回答

0

我注意到,重複的「圖像」對象,你別說加印第二個裏面,所以我找到了解決辦法通過這個JSON對象進行迭代,並採取僅對每個第一要素迭代。 在這一個看看:http://jsfiddle.net/csdtesting/527oevbc/

var data = { 
 
     "result": [ 
 
     [{ 
 
      "Image": { 
 
      "__type": "File", 
 
      "name": "tfss-772aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg", 
 
      "url": "http://f iles.parsetfss.com/49a11e0f-0c84-417e-b81b-58f5e97abae9/tfss-772aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg" 
 
      }, 
 
      "Title": "The Expendables 31", 
 
      "featured_actors": " Sylvester Stallone, Jason Statham, Jet Li", 
 
      "genre": "Action, Adventure, Thril ler", 
 
      "imdb_rating": 6.3, 
 
      "plot_summary": "Barney augments his team with new blood for a personal battle: to take down Conra d Stonebanks, the Expendables co-founder and notorious arms trader who is hell bent on wiping out Barney and every singl e one of his associates.", 
 
      "running_time_mins": 126, 
 
      "trailer_video_link": "http://www.imdb.com/rg/VIDEO_PLAY/LINK//video/im db/vi1896197145/", 
 
      "viewer_rating": "18", 
 
      "objectId": "8cTkg769gM", 
 
      "createdAt": "2014-08-16T21:32:28.318Z", 
 
      "updatedAt": "2014- 08-16T21:35:56.217Z", 
 
      "__type": "Object", 
 
      "className": "Movie" 
 
     }], 
 
     [{ 
 
      "Image": { 
 
      "__type": "File", 
 
      "name": "tfss-65707002-e78b-47bd- 9c7a-da072f388e25-deliver.jpg", 
 
      "url": "http://files.parsetfss.com/49a11e0f-0c84-417e-b81b-58f5e97abae9/tfss-65707002-e78b -47bd-9c7a-da072f388e25-deliver.jpg" 
 
      }, 
 
      "Title": "Deliver Us From Evil", 
 
      "featured_actors": "Eric Bana, Édgar Ramírez, Oliv ia Munn", 
 
      "genre": "Crime, Horror, Thriller", 
 
      "imdb_rating": 6.5, 
 
      "plot_summary": "New York police officer Ralph Sarchie inves tigates a series of crimes. He joins forces with an unconventional priest, schooled in the rites of exorcism, to combat the possessions that are terrorizing their city.", 
 
      "running_time_mins": 118, 
 
      "trailer_video_link": "http://www.imdb.com/vide o/imdb/vi1746054425/?ref_=tt_ov_vi", 
 
      "viewer_rating": "15", 
 
      "objectId": "BoCM1zbOvh", 
 
      "createdAt": "2014-08-25T14:27:24.331Z", 
 
      "updatedAt": "2014-08-25T19:49:09.910Z", 
 
      "__type": "Object", 
 
      "className": "Movie" 
 
     }, { 
 
      "Image": { 
 
      "__type": "File", 
 
      "name": "tfss-772 aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg", 
 
      "url": "http://files.parsetfss.com/49a11e0f-0c84-417e-b81b-58f5e97abae 9/tfss-772aedb6-2767-4935-88f8-ae9280bc512b-expendables.jpg" 
 
      }, 
 
      "Title": "The Expendables 3", 
 
      "featured_actors": " Sylvester Stallone, Jason Statham, Jet Li", 
 
      "genre": "Action, Adventure, Thriller", 
 
      "imdb_rating": 6.3, 
 
      "plot_summary": "Barney augments his team with new blood for a personal battle: to take down Conrad Stonebanks, the Expendables co-founder and notorious arms trader who is hell bent on wiping out Barney and every single one of his associates.", 
 
      "running_time_mins": 126, 
 
      "tra iler_video_link": "http://www.imdb.com/rg/VIDEO_PLAY/LINK//video/imdb/vi1896197145/", 
 
      "viewer_rating": "18", 
 
      "objectId": "8cT kg769gM", 
 
      "createdAt": "2014-08-16T21:32:28.318Z", 
 
      "updatedAt": "2014-08-16T21:35:56.217Z", 
 
      "__type": "Object", 
 
      "className": "Mo vie" 
 
     }] 
 
     ] 
 
    } 
 
    var jsonArray = data.result; 
 
    $.each(jsonArray, function(index, data) { 
 
     //adds all this values fron the array ,only from and to 
 
     //console.log(data[0]); 
 
     //alert(data[0].Title); 
 
     //console.log(data[0].Title); 
 
     document.write("-->" + data[0].Title); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
希望這正是你想要的!

+0

我不明白你的解決方案。另外,您將標題編輯爲不同。這是爲什麼? – user3679294 2014-10-04 21:42:03

+0

我用http://jsonviewer.stack.hu/格式化.i沒有改變任何東西。取消註釋console.log來查看您將瞭解的對象 – 2014-10-04 21:44:46

+0

是否存在Underscore.js實現?因爲,那就是我受到限制,$(#選項)無法識別。 – user3679294 2014-10-04 21:50:41