如何从本地JSON文件解析数据并将其保存在模型类中并在tableview中使用 [英] how to parse data from local JSON file and save in a model class and use in tableview

查看:152
本文介绍了如何从本地JSON文件解析数据并将其保存在模型类中并在tableview中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从本地JSON文件解析JSON,并希望将该数据保存在模型类中并在tableview上显示.我如何从本地文件解析JSON.

i want to parse JSON from local JSON file and want to save that data in a model class and the show it on tableview. how i can parse JSON from local file.

这是我的JSON文件-JsonFile.json

here is my JSON file-- JsonFile.json

{
"teams":[
         {
         "teamName":"Arsenal",
         "image":"Arsenal",
         "nextMatch":"in 2 days",
         "matches":[
                    {
                    "oppositeTeam":"teamName",
                    "matchTimings":"121212",
                    "matchId":"ID 213432"
                    },
                    {
                    "oppositeTeam":"teamName",
                    "matchTimings":"121212",
                    "matchId":"ID 213432"
                    }
                    ],
         "fixtures":{
         "oppositeTeam":"teamName",
         "oppositeTeamScore":"7",
         "homeTeamScore":"4",
         "homeTeamCards":"True",
         "oppositeTeamCards":"false",
         "fixtureId":"ID 213432"
         }
         },
         {
         "teamName":"Chelsea",
         "image":"Chelsea",
         "nextMatch":"in 2 days",
         "matches":{
         "oppositeTeam":"teamName",
         "matchTimings":"121212",
         "matchId":"ID 213432"
         },
         "fixtures":{
         "oppositeTeam":"teamName",
         "oppositeTeamScore":"7",
         "homeTeamScore":"4",
         "homeTeamCards":"True",
         "oppositeTeamCards":"false",
         "fixtureId":"ID 213432"
         }
         },
         {
         "teamName":"India",
         "image":"India",
         "nextMatch":"in 2 days"
         }
         ]

}

这是我的模型类,我要在其中存储JSON文件中的数据.

here is my model class where i wan to store data from JSON file.

   class TeamData : Decodable{
let teamName : String
let image : String
let nextMatch : String?
let matches : [Match]
let fixtures : [Fixture]

}

class Match : Decodable{
let oppositeTeam : String?
let matchTimings : String?
let matchId : String?

}

class Fixture : Decodable{
let oppositeTeam : String?
let oppositeTeamScore : String?
let HomeTeamScore : String?
let HomeTeamCards : String?
let oppositeTeamCards : String?
let fixtureId : String?

}

现在如何解析JSON文件中的数据并将其保存在模型类中..我看过很多教程,但是每个教程都在使用API​​,因此,如何从本地文件解析JSON确实令人困惑

now how to parse data from JSON file and save it on model class.. i have seen a lots of tutorials but every one is using API so its really confusing how to parse JSON from local file

推荐答案

为TeamData修复json结构和类

更改TeamData类以使其与json匹配

Change your TeamData class to match with your json

class TeamData: Decodable {
    var teamName : String
    var image : String
    var nextMatch : String?
    var matches : [Match]?
    var fixtures : Fixtures?
}

对于切尔西队,您忘了将比赛安排在阵列上,因此按以下步骤解决问题:

for team Chelsea you forget to put match to array, so fix it like this:

[{
     "oppositeTeam":"teamName",
     "matchTimings":"121212",
     "matchId":"ID 213432"
}]

我认为,最好的办法是,如果您的json文件中仅有的东西是TeamData对象的数组.因此,删除

I think, the best would be if only thing in your json file was array of TeamData objects. So, delete

{ "teams":

最后是

},并只保留一组团队

and } in the end and keep just array of teams

如何从json文件中获取json

在ViewController中的某个地方创建空的TeamData数组

Somewhere in your ViewController create empty array of TeamData

 var teams = [TeamData]()

现在获得对您的json文件的引用,尝试从中创建数据并使用JSONDecoder对此数据进行解码

Now get reference to your json file, try to create Data from it and decode this data it using JSONDecoder

let url = Bundle.main.url(forResource: "JsonFile", withExtension: "json")!
do {
    let data = try Data(contentsOf: url)
    teams = JSONDecoder().decode([TeamData].self, from: data)
} catch {
    print(error)
}

我还建议您将文件重命名为 Teams.json

I also recommend you to rename your file to Teams.json

也不要忘记在获取文件URL的行中对其重命名

这篇关于如何从本地JSON文件解析数据并将其保存在模型类中并在tableview中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆