简体   繁体   English

如何格式化JSON数据

[英]How to format json data

I'm new to json and I'm really struggling with my data format. 我是JSON的新手,我真的在为数据格式苦苦挣扎。
It needs to have the format: music.genre.album , but just genre.album would also be OK. 它需要具有以下格式: music.genre.album ,但是只要genre.album也可以。

Please could someone help with the following structure? 请有人可以帮助以下结构吗? I will be using getJSON to iterate through it. 我将使用getJSON对其进行迭代。

{"music": [{

"Genre_Blues": [{
    "Album": "224",
    "Path": "images/library/",
    "FileName": "AlbumCover224.png"
    },
    {
    "Album": "277",
    "Path": "images/library/",
    "FileName": "AlbumCover277.png"
    },
    {
    "Album": "885",
    "Path": "images/library/",
    "FileName": "AlbumCover885.png"
    }],
"Genre_Jazz": [{
    "Album": "442",
    "Path": "images/library/",
    "FileName": "AlbumCover442.png"
    },
    {
    "Album": "467",
    "Path": "images/library/",
    "FileName": "AlbumCover467.png"
    },
    {
    "Album": "724",
    "Path": "images/library/",
    "FileName": "AlbumCover724.png"
    }],
"Genre_Rock": [{
    "Album": "022",
    "Path": "images/library/",
    "FileName": "AlbumCover022.png"
    },
    {
    "Album": "609",
    "Path": "images/library/",
    "FileName": "AlbumCover609.png"
    },
    {
    "Album": "067",
    "Path": "images/library/",
    "FileName": "AlbumCover067.png"
    }]
  }]

}

JSON is just like JavaScript (because it is). JSON就像JavaScript(因为它一样)。

If you want to store multiple things inside of genres (which are what genres are...), then it needs to be an array. 如果您想在流派中存储多种内容(流派是...),那么它需要是一个数组。

But think about regular JS objects and arrays. 但是考虑一下常规的JS对象和数组。

music = {};
music.genre = {};
music.genre.blues = [];
music.genre.blues[0] = {};

Can be written like: 可以这样写:

json = { 
    music : {
        genre : {
            blues : [],
            rock  : [],
            soul  : []
        }
    }
}

Then you've got: 然后您将获得:

json.music.genre.blues[0].album_title;  // "Some Kind of Blue"
json.music.genre.blues[0].album_artist; // "Miles Davis"

You could set a loop to go through your blues, or your rock or whatever... 您可以设置一个循环来遍历忧郁,摇滚或其他任何事物。

If you don't want to loop them, then you've got to find some other way to index them, because you can't have an object that has 8 "album" properties. 如果您不想循环它们,那么您必须找到其他索引它们的方法,因为您不能拥有具有8个“相册”属性的对象。 Maybe you could list them by title at that point... ...but that's not all that helpful. 也许那时您可以按标题列出它们... ...但这还不是全部。 You're just getting rid of the array for kicks at that point. 您只是在这一点上摆脱了踢踢的阵地。

You could also set it up so that genre is an array (which is what you have, now). 您还可以对其进行设置,以使体裁是一个数组(现在是这种数组)。

So you might have: 所以你可能有:

music = {
    genre : [
        {
            name : "Blues",
            albums : [
                {
                    title : "Some Kind of Blue",
                    artist : "Miles Davis"
                }
             ]
         },  {
             name : "Soul",
             albums : [
                 {
                     title : "Innervisions",
                     artist : "Stevie Wonder"
                 }, {
                     title : "Talking Book",
                     artist : "Stevie Wonder"
                 }
             ]
         }
     ]
};

Then you've got: 然后您将获得:

music.genre[0].name;             // "Blues"
music.genre[1].albums[0].artist; // "Stevie Wonder"

The benefit of this is to say: 这样做的好处是:

music.genre.forEach(function (genre) { console.log(genre.name); }); // "Blues", "Soul"

music.genre.forEach(function (genre) {
    console.log("---" + genre.name + "---");
    genre.albums.forEach(function (album) {
        console.log("Title: " + album.title);
        console.log("Artist: " + album.artist);
    });
});

It all comes down to what you want to do with it. 这一切都取决于您要使用它来做什么。 You've got the freedom to do anything you want, as long as you know what that is. 只要您知道那是什么,您就可以自由地做任何您想做的事情。

It's almost there. 快到 All you need to do is take out the [ and ] 您需要做的就是取出[]

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

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