如何从字典数组中获取数据-Swift

时间:2018-11-12 06:43:53

标签: ios arrays swift dictionary multidimensional-array

如何从该数组中获取数据? 这里有一个包含一些键值对的数组,有些键包含一个字典数组。

 var dataArray = [
                ["teamName":"Arsenal",
                  "image":"imageName",
                  "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",
                                 "fixturesId":"ID 213432"],

                            ]
    ],["teamName":"Chelsea",
        "image":"imageName",
       "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",
                       "fixturesId":"ID 213432"],["oppositeTeam":"teamName",
                                                  "oppositeTeamScore":"7",
                                                  "HomeTeamScore":"4",
                                                  "HomeTeamCards":"True",
                                                  "oppositeTeamCards":"false",
                                                  "fixturesId":"ID 213432"]
        ]
    ],["teamName":"India",
        "image":"imageName",
       "nextMatch":"null",
       "matches":[],
       "fixtures":[]
    ]]

我尝试过,但是无法从该数组中获取数据。

4 个答案:

答案 0 :(得分:3)

在这里,您可以访问jmeter.save.saveservice.subresults=false 中定义的数组/字典:

dataArray

如果您只是在制作原型,那没关系。如果您打算将其扩展到实际的应用程序中,则最好创建单独的模型。

您可以拥有一个团队模型,其中可以包含团队名称,图像以及比赛和固定装置。对于匹配,您可以创建一个包含匹配信息的模型。同样,您也可以为灯具创建一个模型。然后,您的Team类将包含Match和Fixture类的数组,如下所示:

    // To access team object at zero index
    if let team = dataArray[0] as? [String: Any] {
        print("Team: \(team["teamName"])")

        // To access matches array of team object at zero index
        if let matches = team["matches"] as? [[String: Any]] {
            print( matches)

            // To access first match
            if let match = matches.first {
                print(match)
            }
        }

        // Similar to matches access fixtures
        if let fixtures = dataArray[0]["fixtures"] as? [[String: Any]] {
            print(fixtures)

            // To access first fixture
            if let fixture = fixtures.first {
                print(fixture)
            }
        }
    }

,并且您的var matches: [Match] var fixtures: [Fixture] 将是

类型
dataArray

答案 1 :(得分:3)

您需要使用模型

struct Team {
    let teamName:String
    let image:String
    let nextMatch:String
    let matches:[Match]?
    let fixtures:[Fixture]?
}

struct Match {
    let oppositeTeam:String
    let matchTimings:String
    let matchId:String
}

struct Fixture {
    let oppositeTeam:String
    let oppositeTeamScore:String
    let HomeTeamScore:String
    let HomeTeamCards:String
    let oppositeTeamCards:String
    let fixturesId:String
}

接下来,您需要快速了解Codeable,我已在下面附上了文章

Codeable Tutorial in swift

答案 2 :(得分:1)

使用Codable为您的数据创建模型。使用JSON解码器解析模型中的数据。然后,您可以在任何地方使用模型。

对于JSON解析,您可以参考本教程: default schema

答案 3 :(得分:1)

您可以像这样从阵列中获取数据:

@CreateJSONTuple =
SELECT   JsonFunctions.JsonTuple(Employee).Values AS EmployeeData,
         JsonFunctions.JsonTuple(Info) AS InfoData
FROM @json;
@result =
SELECT JsonFunctions.JsonTuple(employee)["name"] AS Name,
       JsonFunctions.JsonTuple(InfoData, "location") AS LocationData
FROM @CreateJSONTuple
CROSS APPLY
    EXPLODE(EmployeeData) AS employees(employee) ;