1

私は JS の学習過程にあり、先生は私たちの JS で JSON ファイルからの情報を使用するという割り当てを与えました。

問題は、JSON を JavaScript の変数/配列に変換する方法がわからないため、既存の JS コードで使用できることです。

私のJSONは次のようになります。

    "id": 0,
    "albumName":"Greatest hits",
    "artistName":"ZZ-top",
    "artistWebsite":"http://www.zztop.com/",
    "productionYear": 1992,     
    "trackList":[
        {
            "trackNumber":1,
            "trackTitle":"Gimme all your lovin'",
            "trackTimeInSeconds":241
        },

        {
            "trackNumber":2, 
            "trackTitle":"sharp dressed man",
            "trackTimeInSeconds":256
        },

        {
            "trackNumber":3,
            "trackTitle":"Rough boy",
            "trackTimeInSeconds":290
        },

        {
            "trackNumber":4,
            "trackTitle":"Tush",
            "trackTimeInSeconds":135
        },

        {
            "trackNumber":5,
            "trackTitle":"My head's in Mississippi",
            "trackTimeInSeconds":261
        },

        {
            "trackNumber":6,
            "trackTitle":"Pearl Necklace",
            "trackTimeInSeconds":246
        },

        {
            "trackNumber":7,
            "trackTitle":"I'm bad, I'm nationwide",
            "trackTimeInSeconds":287
        },

        {
            "trackNumber":8,
            "trackTitle":"Viva Las Vegas",
            "trackTimeInSeconds":288
        },

        {
            "trackNumber":9,
            "trackTitle":"Doubleback",
            "trackTimeInSeconds":236
        },

        {
            "trackNumber":10,
            "trackTitle":"Gun Love",
            "trackTimeInSeconds":220
        },

        {
            "trackNumber":11,
            "trackTitle":"Got me under pressure",
            "trackTimeInSeconds":241
        },

        {
            "trackNumber":12,
            "trackTitle":"Give it up",
            "trackTimeInSeconds":210
        },

        {
            "trackNumber":13,
            "trackTitle":"Cheap sunglasses",
            "trackTimeInSeconds":287
        },

        {
            "trackNumber":14,
            "trackTitle":"Sleeping bag",
            "trackTimeInSeconds":244
        },

        {
            "trackNumber":15,
            "trackTitle":"Planet of women",
            "trackTimeInSeconds":246
        },

        {
            "trackNumber":16,
            "trackTitle":"La Grange",
            "trackTimeInSeconds":233
        },

        {
            "trackNumber":17,
            "trackTitle":"Tube snake boogie",
            "trackTimeInSeconds":185
        },

        {
            "trackNumber":18,
            "trackTitle":"Legs-remix version",
            "trackTimeInSeconds":272
        }
    ]
},

そして、この変数に入る必要があります:

    artistName : "ArtistName",
    albumName : "AlbumName",
    noOfTracks : 0,
    prodYear : 9999,
    trackList : "",

    init : function(artistName, albumName, noOfTracks, prodYear, trackList ){
        this.artistName = artistName;
        this.albumName = albumName;
        this.noOfTracks = noOfTracks;
        this.prodYear = prodYear;
        this.trackList = trackList;

        return this;
    },
4

1 に答える 1