在Swift 4中解析JSON
我正在开发一个应用程序,从NiceHash api中获得平衡。我必须处理的JSON如下所示:在Swift 4中解析JSON,json,swift,swift4,Json,Swift,Swift4,我正在开发一个应用程序,从NiceHash api中获得平衡。我必须处理的JSON如下所示: { "result":{ "stats":[ { "balance":"0.00000124", "rejected_speed":"0", "algo":5, "accepted_speed":"0" }, {
{
"result":{
"stats":[
{
"balance":"0.00000124",
"rejected_speed":"0",
"algo":5,
"accepted_speed":"0"
},
{
"balance":"0.00000163",
"rejected_speed":"0",
"algo":7,
"accepted_speed":"0"
},
{
"balance":"0.00114271",
"rejected_speed":"0",
"algo":8,
"accepted_speed":"0.0002237"
},
{
"balance":"0.00009395",
"rejected_speed":"0",
"algo":14,
"accepted_speed":"0"
},
{
"balance":"0.0000097",
"rejected_speed":"0",
"algo":20,
"accepted_speed":"0"
},
{
"balance":"0.00000004",
"rejected_speed":"0",
"algo":21,
"accepted_speed":"0"
},
{
"balance":"0.00128791",
"rejected_speed":"0",
"algo":22,
"accepted_speed":"0"
},
{
"balance":"0.0000005",
"rejected_speed":"0",
"algo":23,
"accepted_speed":"0"
},
{
"balance":"0.00310707",
"rejected_speed":"0",
"algo":24,
"accepted_speed":"0.0000002"
},
{
"balance":"0.00002411",
"rejected_speed":"0",
"algo":26,
"accepted_speed":"0"
},
{
"balance":"0.00000007",
"rejected_speed":"0",
"algo":27,
"accepted_speed":"0"
},
{
"balance":"0.0000047",
"rejected_speed":"0",
"algo":28,
"accepted_speed":"0"
},
{
"balance":"0.00001291",
"rejected_speed":"0",
"algo":29,
"accepted_speed":"0"
}
],
"payments":[
],
"addr":"some_bitcoin_adress"
},
"method":"stats.provider"
}
struct stats: Codable {
let balance: String
let rejected_speed: String
let algo: String
let accepted_speed: String
}
struct result: stats {
let stats: stats
}
我想对“balance”的所有值求和,但我不确定如何访问它。我现在的代码如下所示:
{
"result":{
"stats":[
{
"balance":"0.00000124",
"rejected_speed":"0",
"algo":5,
"accepted_speed":"0"
},
{
"balance":"0.00000163",
"rejected_speed":"0",
"algo":7,
"accepted_speed":"0"
},
{
"balance":"0.00114271",
"rejected_speed":"0",
"algo":8,
"accepted_speed":"0.0002237"
},
{
"balance":"0.00009395",
"rejected_speed":"0",
"algo":14,
"accepted_speed":"0"
},
{
"balance":"0.0000097",
"rejected_speed":"0",
"algo":20,
"accepted_speed":"0"
},
{
"balance":"0.00000004",
"rejected_speed":"0",
"algo":21,
"accepted_speed":"0"
},
{
"balance":"0.00128791",
"rejected_speed":"0",
"algo":22,
"accepted_speed":"0"
},
{
"balance":"0.0000005",
"rejected_speed":"0",
"algo":23,
"accepted_speed":"0"
},
{
"balance":"0.00310707",
"rejected_speed":"0",
"algo":24,
"accepted_speed":"0.0000002"
},
{
"balance":"0.00002411",
"rejected_speed":"0",
"algo":26,
"accepted_speed":"0"
},
{
"balance":"0.00000007",
"rejected_speed":"0",
"algo":27,
"accepted_speed":"0"
},
{
"balance":"0.0000047",
"rejected_speed":"0",
"algo":28,
"accepted_speed":"0"
},
{
"balance":"0.00001291",
"rejected_speed":"0",
"algo":29,
"accepted_speed":"0"
}
],
"payments":[
],
"addr":"some_bitcoin_adress"
},
"method":"stats.provider"
}
struct stats: Codable {
let balance: String
let rejected_speed: String
let algo: String
let accepted_speed: String
}
struct result: stats {
let stats: stats
}
以及获取JSON的函数:
func updateBalance() {
let sampleDataAdress = "nicehash_link_here"
let url URL(string: sampleDataAdress)!
let jsonSata = try! Data(contentsOf: url)
let jsonDecoder = JSONDecoder()
let data = try? jsonDecoder.decode(Array<stats>.self, from jsonData)
data?.count
dump(data?.first)
}
func updateBalance(){
让SampleDataAddress=“niceshash\u链接\u此处”
让url(字符串:SampleDataAddress)!
让jsondata=try!数据(contentsOf:url)
让jsonDecoder=jsonDecoder()
让data=try?jsonDecoder.decode(Array.self,来自jsonData)
数据?计数
转储(数据?.first)
}
我希望我至少能得到第一个数据,但我得到的却是零。这是我第一次在Swift 4中使用JSON,我想这段代码只是垃圾。有很多问题
首先,为了避免混淆,结构的名称总是大写的。但是,您的
stats
struct包含的混乱不仅仅是命名。以大写字母开始命名结构是Swift命名惯例。顺便说一句,您应该使用URLSession异步下载json数据。如果您对algo属性使用了错误的类型,那么它应该是Int而不是字符串。您应该添加结构,该结构的名称为“Response”,并且该结构必须包含“result”结构的属性。最后,使用jsonDecoder.decode(Response.self,来自jsonData)而不是jsonDecoder.decode(Array.self,来自jsonData)解析“Response”结构,而不是使用try?
忽略抛出的错误,在do catch块中使用try
,并打印抛出的错误。它会确切地告诉您问题是什么。为什么不可编码?似乎这些对象永远不会被编码。您还可以向Resultvar sum:Double{return stats.flatMap{Double($0.balance)}.reduce(0,+)}
添加一个计算属性,类似吗.0
它不是needed@LeoDabus我知道这一点,但为了更好的可读性,我总是使用正确的文本类型。