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

0

首先制作一个字符串:

var ab = JSON.stringify(...);  // pass your json here

注意:在控制台上试试这个。

然后制作一个对象:

var ac = JSON.parse(ab);

现在你有了一个 JavaScript 对象,你可以使用:

    ac.artistName = artistName;
    ac.albumName = albumName;
    ac.noOfTracks = noOfTracks;
    ac.prodYear = prodYear;
    ac.trackList = trackList;

希望这可以帮助。:)

于 2013-10-31T10:46:35.740 回答