gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:14:31 25 4
gpt4 key购买 nike

我正在离线模式下在 ios native 中实现歌曲(仅限文本)应用程序。我正在创建本地 JSON 文件以从这些文件中读取数据。我正在使用以下 JSON 文件,但我不知道它是否是开发此应用程序的正确格式。但我想知道当用户单击“下一个”和“上一个”按钮时如何根据 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 类/结构,然后将 JSON 数据存储在数组中。然后,您可以根据下一个上一个按钮使用增加数组索引 +1、-1。

关于ios - 如何在ios swift中根据ID读取本地json文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59316448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com