2017-01-09 55 views
-1

我有以下JSON文件的一部分。我需要使用純Javascript來轉換爲Javascript數組。具體而言,我需要有一個陣列 userid,movieid,rating,timestamp只對於rating大於4.0使用純Javascript將JSON轉換爲Javascript數組

[{ 
    "rating": 3.0, 
    "ratingsPK": { 
     "movieId": 2017, 
     "userId": 610 
    }, 
    "timestamp": 913815387 
}, { 
    "rating": 5.0, 
    "ratingsPK": { 
     "movieId": 2017, 
     "userId": 654 
    }, 
    "timestamp": 959098930 
}, { 
    "rating": 4.0, 
    "ratingsPK": { 
     "movieId": 2017, 
     "userId": 856 
    }, 
    "timestamp": 1059863336 
}, { 
    "rating": 2.0, 
    "ratingsPK": { 
     "movieId": 2017, 
     "userId": 903 
    }, 
    "timestamp": 903628492 
}, { 
    "rating": 3.5, 
    "ratingsPK": { 
     "movieId": 2017, 
     "userId": 1185 
    }, 
    "timestamp": 1100560205 
}] 
+3

請加通緝的結果,以及,你都試過了。 –

+0

使用'JSON.parse,Array.forEach並將其推送到數組'。檢查我的答案 –

回答

0

您可以閱讀使用XMLHttpRequestJSON文件,並且將返回string和你想象中的變量有文件數據。 jsonFile

使用JSON.parse像下面。

var rating = JSON.parse(jsonFile); 

以下是工作示例。

var rating = [ 
 
     {"rating":3.0,"ratingsPK":{"movieId":2017,"userId":610},"timestamp":913815387}, 
 
     {"rating":5.0,"ratingsPK":{"movieId":2017,"userId":654},"timestamp":959098930}, 
 
     {"rating":4.0,"ratingsPK":{"movieId":2017,"userId":856},"timestamp":1059863336}, 
 
     {"rating":2.0,"ratingsPK":{"movieId":2017,"userId":903},"timestamp":903628492}, 
 
     {"rating":3.5,"ratingsPK":{"movieId":2017,"userId":1185},"timestamp":1100560205} 
 
]; 
 
    
 
    var bestRated = []; 
 
    
 
    rating.forEach(function (item) { 
 
     if(item.rating > 4) { 
 
     bestRated.push({ 
 
      userid: item.ratingsPK.userId, 
 
      movieid: item.ratingsPK.movieId, 
 
      rating: item.rating, 
 
      timestamp: item.timestamp 
 
     }); 
 
     } 
 
    }); 
 
    
 
    console.log('bestRated: ', bestRated);

3

您可以使用JSON.parse()將JSON字符串轉換爲JavaScript對象。然後用適當的謂詞使用Array#filter來得到你所需要的(在你的情況下,timestamp大於4)。

1

簡單使用JSON.parse()Array.prototype.filter()函數 「一行」 溶液:

var json_data = '[{"rating":3.0,"ratingsPK":{"movieId":2017,"userId":610},"timestamp":913815387},{"rating":5.0,"ratingsPK":{"movieId":2017,"userId":654},"timestamp":959098930},{"rating":4.0,"ratingsPK":{"movieId":2017,"userId":856},"timestamp":1059863336},{"rating":2.0,"ratingsPK":{"movieId":2017,"userId":903},"timestamp":903628492},{"rating":3.5,"ratingsPK":{"movieId":2017,"userId":1185},"timestamp":1100560205}]'; 
 
    
 
var arr = JSON.parse(json_data).filter(function(o){ return o.rating >= 4; }); 
 
console.log(arr);

相同的,但在EcmaScript6方式(使用箭頭功能):

var json_data = '[{"rating":3.0,"ratingsPK":{"movieId":2017,"userId":610},"timestamp":913815387},{"rating":5.0,"ratingsPK":{"movieId":2017,"userId":654},"timestamp":959098930},{"rating":4.0,"ratingsPK":{"movieId":2017,"userId":856},"timestamp":1059863336},{"rating":2.0,"ratingsPK":{"movieId":2017,"userId":903},"timestamp":903628492},{"rating":3.5,"ratingsPK":{"movieId":2017,"userId":1185},"timestamp":1100560205}]'; 
 
     
 
var arr = JSON.parse(json_data).filter((o) => o.rating >= 4); 
 
console.log(arr);

1

轉換爲對象和濾波等級後,你可以得到一個陣列,只取對象的部分,你需要的。

var data = '[{"rating":3.0,"ratingsPK":{"movieId":2017,"userId":610},"timestamp":913815387},{"rating":5.0,"ratingsPK":{"movieId":2017,"userId":654},"timestamp":959098930},{"rating":4.0,"ratingsPK":{"movieId":2017,"userId":856},"timestamp":1059863336},{"rating":2.0,"ratingsPK":{"movieId":2017,"userId":903},"timestamp":903628492},{"rating":3.5,"ratingsPK":{"movieId":2017,"userId":1185},"timestamp":1100560205}]', 
 
    result = JSON.parse(data).filter(function (a) { 
 
     return a.rating > 4; 
 
    }).map(function (a) { 
 
     return [a.ratingsPK.userId, a.ratingsPK.movieId, a.rating, a.timestamp]; 
 
    }); 
 

 
console.log(result);

0

JSON.parse()來可用於JSON轉換爲JS對象。以下是適合您需求的實例。

var data='[{"rating":1.0,"ratingsABC":{"movieId":2017,"userId":123},"timestamp":913815387},{"rating":1.0,"ratingsABC":{"movieId":2017,"userId":123},"timestamp":913815387},{"rating":1.0,"ratingsABC":{"movieId":2017,"userId":123},"timestamp":913815387}]'; 
var sortedData= JSON.parse(data).filter((a) => a.rating >= 4); 
0

這是不使用任何花哨的API:

var jsonArr = [ 
    {"rating":3.0,"ratingsPK":{"movieId":2017,"userId":610},"timestamp":913815387}, 
    {"rating":5.0,"ratingsPK":{"movieId":2017,"userId":654},"timestamp":959098930}, 
    {"rating":4.0,"ratingsPK":{"movieId":2017,"userId":856},"timestamp":1059863336}, 
    {"rating":2.0,"ratingsPK":{"movieId":2017,"userId":903},"timestamp":903628492}, 
    {"rating":3.5,"ratingsPK":{"movieId":2017,"userId":1185},"timestamp":1100560205}]; 

var arr = []; 

for(var x in jsonArr){ 
    if(jsonArr[x]['rating'] > 4.0){ 
     arr.push(jsonArr[x]['ratingsPK']['userId']); 
     arr.push(jsonArr[x]['ratingsPK']['movieId']); 
     arr.push(jsonArr[x]['rating']); 
     arr.push(jsonArr[x]['timestamp']); 
    } 
} 
console.log(arr);