使用 swiftyJSON/swift 从 Firebase 解析排行榜数据
Posted
技术标签:
【中文标题】使用 swiftyJSON/swift 从 Firebase 解析排行榜数据【英文标题】:Parsing leaderboard data from Firebase, using swiftyJSON/swift 【发布时间】:2016-03-06 02:13:11 【问题描述】:我正在尝试从 Firebase 中提取排行榜数据。当我查看“快照”数据时,我能够以正确的顺序获取数据。一旦我到达 var json = JSON(snapshot.value) 的行,数据就会失去其顺序。
我正在尝试将数据解析为 swift 对象,以便它们可以在我的 swift 应用程序中作为有序排行榜使用/显示。我可以很容易地找到我的数据出现故障的解决方法,但这让我很困扰。
func getScoresData()
let scoresRef = ref.childByAppendingPath("/High Scores")
scoresRef.queryLimitedToFirst(10).observeSingleEventOfType(.Value, withBlock: snapshot in
print(snapshot)
var json = JSON(snapshot.value)
print(json)
let theKeys = json.dictionary!.keys
for key in theKeys
self.timeStampArray.append(key)
let tempName = json[key]["Name"]
self.namesArray.append(String(tempName))
let tempScore = json[key]["Score"]
self.scoresArray.append(String(tempScore))
)
这是我从打印(快照)中正确排序的输出:
Snap (High Scores)
"-KC3MktE1jS5z9-tTEgJ" =
".priority" = "-9";
Date = "1457140993.669797";
Name = brendan;
Score = 9;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
;
"-KC3MktM-rWmoitem8na" =
".priority" = "-9";
Date = "1457140993.677347";
Name = brendan;
Score = 9;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
;
"-KC3MktW0iJfkAKjlCy4" =
".priority" = "-9";
Date = "1457140993.686348";
Name = brendan;
Score = 9;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
;
"-KC3MktapH6Mebw2sAUe" =
".priority" = "-10";
Date = "1457140993.692581";
Name = brendan;
Score = 10;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
;
"-KC3MktbOyltGaJZ_XtD" =
".priority" = "-10";
Date = "1457140993.694163";
Name = brendan;
Score = 10;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
;
"-KC3MktjFj6VVAJfitZp" =
".priority" = "-10";
Date = "1457140993.701368";
Name = brendan;
Score = 10;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
;
"-KC3Mktk0_f6_eM8wP1x" =
".priority" = "-10";
Date = "1457140993.702492";
Name = brendan;
Score = 10;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
;
"-KC3MktrZaStt9b5pID_" =
".priority" = "-10";
Date = "1457140993.709843";
Name = brendan;
Score = 10;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
;
"-KC3QALPl38gbDy4xLE2" =
".priority" = "-10";
Date = "1457141888.477473";
Name = brendan;
Score = 10;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
;
"-KC3qGAqZiJ9Vs40MwG7" =
".priority" = "-10";
Date = "1457148990.24583";
Name = brendan;
Score = 10;
UUID = "BD561F1C-72A8-4A01-A71A-850682E49D61";
;
这是我在 print(json) 行中排序不正确的数据:
"-KC3MktrZaStt9b5pID_" :
"Score" : "10",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.709843,
"Name" : "brendan"
,
"-KC3QALPl38gbDy4xLE2" :
"Score" : 10,
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457141888.477473,
"Name" : "brendan"
,
"-KC3MktW0iJfkAKjlCy4" :
"Score" : "9",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.686348,
"Name" : "brendan"
,
"-KC3MktbOyltGaJZ_XtD" :
"Score" : "10",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.694163,
"Name" : "brendan"
,
"-KC3Mktk0_f6_eM8wP1x" :
"Score" : "10",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.702492,
"Name" : "brendan"
,
"-KC3MktM-rWmoitem8na" :
"Score" : "9",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.677347,
"Name" : "brendan"
,
"-KC3MktapH6Mebw2sAUe" :
"Score" : "10",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.692581,
"Name" : "brendan"
,
"-KC3qGAqZiJ9Vs40MwG7" :
"Score" : 10,
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457148990.24583,
"Name" : "brendan"
,
"-KC3MktjFj6VVAJfitZp" :
"Score" : "10",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.701368,
"Name" : "brendan"
,
"-KC3MktE1jS5z9-tTEgJ" :
"Score" : "9",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.669797,
"Name" : "brendan"
由于我使用 json 对象来获取键,从而填充数组,因此数组中的数据是乱序的。
我错过了什么?
【问题讨论】:
JSON 字典键未排序。会不会是这个问题? 这里有几件事。 Firebase 的顺序概念由您定义;您需要一个密钥来让 Firebase 在读入数据时知道如何对其进行排序。如果您想要一个按时间顺序排列的顺序,那么 .orderBy 时间戳。如果你想按分数排序,那么 .orderBy 分数。不要假设您的 Firebase 数据会被排序 - 它们可能是按键排序的,但这可能不是您想要的。要正确回答问题,请定义您希望数据排序的方式,我们将向您展示如何实现。 【参考方案1】:如果您想保持顺序,请使用数组而不是字典,无论是 JSON
还是 Swift 对象。所以为了保持排序,你可以改变你的JSON
结构:
"-KC3MktrZaStt9b5pID_" :
"Score" : "10",
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457140993.709843,
"Name" : "brendan"
,
"-KC3QALPl38gbDy4xLE2" :
"Score" : 10,
"UUID" : "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date" : 1457141888.477473,
"Name" : "brendan"
到这里:
[
"Key": "-KC3MktrZaStt9b5pID_",
"Score": "10",
"UUID": "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date": 1457140993.709843,
"Name": "brendan"
,
"Key": "-KC3QALPl38gbDy4xLE2",
"Score": 10,
"UUID": "BD561F1C-72A8-4A01-A71A-850682E49D61",
"Date": 1457141888.477473,
"Name": "brendan"
]
【讨论】:
我不认为 Firebase 本身支持数组,根据:firebase.com/blog/… 好的,然后保留你的旧语法。博客中关于使用优先级对数据进行排序的部分可能很方便,字典本身没有排序。 @jakhamma NSArray 在 Firebase 中绝对受支持。但是,大多数时候有比使用数组存储数据更好的解决方案。数组可能......有问题,应该只在特定情况下使用,即使这样通常也有更好的选择。以上是关于使用 swiftyJSON/swift 从 Firebase 解析排行榜数据的主要内容,如果未能解决你的问题,请参考以下文章
SwiftyJson Swift 3 无法访问 JSON 中的字符串