如何根据ios swift中的ID读取本地json文件?

How to read local json file based on ID in ios swift?

我正在离线模式下 ios 本机实现歌曲(纯文本)应用程序。我正在创建本地 JSON 文件以从这些文件中读取数据。我正在使用以下 JSON 文件,但我不知道它是否适合开发此应用程序。但是我想知道当用户点击 Next 和 Previous 按钮时如何根据 SongID 读取特定的歌曲标题和文本

我的任务: 1.需要实现NextSong和PreviousSong按钮 2.需要显示歌单标题

{
  “Telugu_songs“:[
     {
       “Id”: 1,
       “Title”: “song1 title”,
       “Text”: “song1 sample text”
     },
     {
       “Id”: 2,
       “Title”: “song2 title”,
       “Text”: “song2 sample text”
     },
    {
       “Id”: 3,
       “Title”: “song3 title”,
       “Text”: “song3 sample text”
     },
  ],

“English_songs“:[
     {
       “Id”: 1,
       “Title”: “song1 title”,
       “Text”: “song1 sample text”
     },
     {
       “Id”: 2,
       “Title”: “song2 title”,
       “Text”: “song2 sample text”
     },
    {
       “Id”: 3,
       “Title”: “song3 title”,
       “Text”: “song3 sample text”
     },
  ],

“Hindi_songs“:[
     {
       “Id”: 1,
       “Title”: “song1 title”,
       “Text”: “song1 sample text”
     },
     {
       “Id”: 2,
       “Title”: “song2 title”,
       “Text”: “song2 sample text”
     },
    {
       “Id”: 3,
       “Title”: “song3 title”,
       “Text”: “song3 sample text”
     },
  ]
}   

您的 JSON 格式是正确的,只是您用来包裹键和值的双引号似乎是错误的。这是你的作品 JSON:

{
"Telugu_songs": [{
        "Id": 1,
        "Title": "song1 title",
        "Text": "song1 sample text"
    },
    {
        "Id": 2,
        "Title": "song2 title",
        "Text": "song2 sample text"
    },
    {
        "Id": 3,
        "Title": "song3 title",
        "Text": "song3 sample text"
    }
],

"English_songs": [{
        "Id": 1,
        "Title": "song1 title",
        "Text": "song1 sample text"
    },
    {
        "Id": 2,
        "Title": "song2 title",
        "Text": "song2 sample text"
    },
    {
        "Id": 3,
        "Title": "song3 title",
        "Text": "song3 sample text"
    }
],

"Hindi_songs": [{
        "Id": 1,
        "Title": "song1 title",
        "Text": "song1 sample text"
    },
    {
        "Id": 2,
        "Title": "song2 title",
        "Text": "song2 sample text"
    },
    {
        "Id": 3,
        "Title": "song3 title",
        "Text": "song3 sample text"
    }
]
}

更新:

创建 Codable class/struct,然后将 JSON 数据存储在数组中。然后,您可以根据下一个上一个按钮使用增加数组索引 +1、-1。