开发者社区> 问答> 正文

如何在iOS swift中根据ID读取本地json文件?

我正在实现歌曲(仅文本)应用程序在iOS本机在离线模式。我正在创建本地JSON文件来读取这些文件中的数据。我正在使用以下JSON文件,但我不知道它是否是开发此应用程序的正确格式。但是,我想知道当用户单击“下一步”和“前面”按钮时,如何根据Song ID读取特定的歌曲标题和文本

我的任务: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”
     },
  ]
}

展开
收起
游客5akardh5cojhg 2019-12-13 14:23:25 1147 0
1 条回答
写回答
取消 提交回答
  • 您的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"
        }
    ]
    }
    
    

    更新:

    创建一个可编码的类/struct,然后将JSON数据存储在数组中。然后,您可以根据下一个按钮使用递增数组索引+1,-1。

    2019-12-13 14:24:18
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载