简体   繁体   English

从.json文件中获取并解析多个arrays

[英]Fetch and parse multiple arrays from .json file

I'm making a collection of all the movies I've watched and I keep all my data in movies.json.我正在收集我看过的所有电影,并将所有数据保存在 movies.json 中。 In movies.json there are multiple arrays, the first one being "MarvelMovies" with 2 movies in.在movies.json中有多个arrays,第一个是“MarvelMovies”,其中有2部电影。

Next is "ComedyMovies" with also 4 movies.接下来是“ComedyMovies”,还有 4 部电影。

I can console.log all the arrays, but I haven't figured out how to console.log each array (console.log the MarvelMovies array and ComedyMovies separately).我可以控制台记录所有 arrays,但我还没有弄清楚如何控制台记录每个数组(分别控制台记录 MarvelMovies 数组和 ComedyMovies)。

movies.json:电影.json:

{
    "MarvelMovies": [{
            "Title": "The Avengers",
            "Year": "2012",
            "Poster": "https://m.media-amazon.com/images/M/MV5BNDYxNjQyMjAtNTdiOS00NGYwLWFmNTAtNThmYjU5ZGI2YTI1XkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_SX300.jpg",
            "Ratings": [{
                "Source": "Internet Movie Database",
                "Value": "8.0/10"
            }, {
                "Source": "Rotten Tomatoes",
                "Value": "91%"
            }, {
                "Source": "Metacritic",
                "Value": "69/100"
            }],
            "Metascore": "69"

            
        }, {


            "Title": "Avengers: Age of Ultron",
            "Year": "2015",
            "Rated": "PG-13",
            "Released": "01 May 2015",
            "Runtime": "141 min",
            "Genre": "Action, Adventure, Sci-Fi",
            "Director": "Joss Whedon",
            "Writer": "Joss Whedon, Stan Lee (based on the Marvel comics by), Jack Kirby (based on the Marvel comics by), Joe Simon (character created by: Captain America), Jack Kirby (character created by: Captain America), Jim Starlin (character created by: Thanos)",
            "Actors": "Robert Downey Jr., Chris Hemsworth, Mark Ruffalo, Chris Evans",
            "Plot": "When Tony Stark and Bruce Banner try to jump-start a dormant peacekeeping program called Ultron, things go horribly wrong and it's up to Earth's mightiest heroes to stop the villainous Ultron from enacting his terrible plan.",
            "Language": "English, Korean",
            "Country": "USA",
            "Awards": "8 wins & 49 nominations.",
            "Poster": "https://m.media-amazon.com/images/M/MV5BMTM4OGJmNWMtOTM4Ni00NTE3LTg3MDItZmQxYjc4N2JhNmUxXkEyXkFqcGdeQXVyNTgzMDMzMTg@._V1_SX300.jpg",
            "Ratings": [{
                "Source": "Internet Movie Database",
                "Value": "7.3/10"
            }, {
                "Source": "Rotten Tomatoes",
                "Value": "75%"
            }, {
                "Source": "Metacritic",
                "Value": "66/100"
            }],
            "Metascore": "66",
            "imdbRating": "7.3",
            "imdbVotes": "759,059",
            "imdbID": "tt2395427",
            "Type": "movie",
            "DVD": "08 Sep 2015",
            "BoxOffice": "$459,005,868",
            "Production": "Marvel Studios",
            "Website": "N/A",
            "Response": "True"
        }


    ],
    "ComedyMovies": [{
            "Title": "The Avengers",
            "Year": "2012",
            "Poster": "https://m.media-amazon.com/images/M/MV5BNDYxNjQyMjAtNTdiOS00NGYwLWFmNTAtNThmYjU5ZGI2YTI1XkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_SX300.jpg",
            "Ratings": [{
                "Source": "Internet Movie Database",
                "Value": "8.0/10"
            }, {
                "Source": "Rotten Tomatoes",
                "Value": "91%"
            }, {
                "Source": "Metacritic",
                "Value": "69/100"
            }],
            "Metascore": "69"

            
        }, {


            "Title": "Avengers: Age of Ultron",
            "Year": "2015",
            "Rated": "PG-13",
            "Released": "01 May 2015",
            "Runtime": "141 min",
            "Genre": "Action, Adventure, Sci-Fi",
            "Director": "Joss Whedon",
            "Writer": "Joss Whedon, Stan Lee (based on the Marvel comics by), Jack Kirby (based on the Marvel comics by), Joe Simon (character created by: Captain America), Jack Kirby (character created by: Captain America), Jim Starlin (character created by: Thanos)",
            "Actors": "Robert Downey Jr., Chris Hemsworth, Mark Ruffalo, Chris Evans",
            "Plot": "When Tony Stark and Bruce Banner try to jump-start a dormant peacekeeping program called Ultron, things go horribly wrong and it's up to Earth's mightiest heroes to stop the villainous Ultron from enacting his terrible plan.",
            "Language": "English, Korean",
            "Country": "USA",
            "Awards": "8 wins & 49 nominations.",
            "Poster": "https://m.media-amazon.com/images/M/MV5BMTM4OGJmNWMtOTM4Ni00NTE3LTg3MDItZmQxYjc4N2JhNmUxXkEyXkFqcGdeQXVyNTgzMDMzMTg@._V1_SX300.jpg",
            "Ratings": [{
                "Source": "Internet Movie Database",
                "Value": "7.3/10"
            }, {
                "Source": "Rotten Tomatoes",
                "Value": "75%"
            }, {
                "Source": "Metacritic",
                "Value": "66/100"
            }],
            "Metascore": "66",
            "imdbRating": "7.3",
            "imdbVotes": "759,059",
            "imdbID": "tt2395427",
            "Type": "movie",
            "DVD": "08 Sep 2015",
            "BoxOffice": "$459,005,868",
            "Production": "Marvel Studios",
            "Website": "N/A",
            "Response": "True"
        }


    ]
}

my js fetch code:我的 js 获取代码:

//DOM element
let movieSlider = document.querySelector(".mainSlider") //Grid til film
let movieTitle = document.querySelector(".mainSlider__title"); //genre titel
fetch('./media/json/movies.json')
.then(response => response.json())
.then((movies) => {
    console.log(movies)
//   console.log(response);
//   });
// .then(movie => {
    for (let i = 0; i < movies.length; i++) {
        const element = movies[i];
        movieSlider.innerHTML += `
        <div class="mainSlider__item" style='background:url("${element.Poster}"); background-size:cover;'>
        <div class="mainSlider__item-playButton">
            <div class="mainSlider__item-title">${element.Title}</div>
            <div class="mainSlider__item-info">${element.Year}</div>
            <div class="mainSlider__item-desc">${element.Runtime}</div>
        </div>
    </div>
        `


    }
});

If you don't know the property names and also you don't need them.如果您不知道属性名称并且也不需要它们。 (They seem like categories). (它们看起来像类别)。 You can use Object.values and Array.flat methods to retrieve an array that contains just movies like below:您可以使用Object.valuesArray.flat方法检索仅包含以下电影的数组:

const json = `your json`
const movies = Object.values(json).flat();
console.log(movies);

 const json = `your json` const movies = Object.values(json).flat(); console.log(movies);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM