gpt4 book ai didi

ios - 如何从Swift 4/json字典中的多值键检索单个值?

转载 作者:行者123 更新时间:2023-12-01 19:43:44 27 4
gpt4 key购买 nike

有很多检索键值的示例,我可以做到这一点。在这种情况下,键具有多个值。关键是“挖掘”,后跟6个值。相同的key:value重复30次,当然,这些key的值也不同。

我似乎无法解决的是如何返回值。尤其是第一个,例如1532825277682891390,它似乎是一个具有与之相关的键/值对的子键。

The todo is: ["mining": {
1532825277682891390 = {
command = mining;
siteid = "Platform Mining";
ts = 1532825270557;
user1 = 73276;
value = 1;
};
1532826406100318457 = {
command = mining;
siteid = "Platform Mining";
ts = 1532826375712;
user1 = 73276;
value = 1;
};
1532827074955562013 = {
command = mining;
siteid = "Platform Mining";
ts = 1532827066645;
user1 = 73276;
value = 1;
};
153282775791322835 = {
command = mining;
siteid = "Platform Mining";
ts = 1532827753205;
user1 = 73276;
value = 1;
};

....
....
....
}

我可以显示“挖掘”键的key:value。字典中只有两个键:“采矿”和“成功”-我并不担心“成功”,它通常返回“1”。

这基本上是带有一些示例和试验输出的代码,因为我尝试了与其他文章不同的事情。我只是无法理解这一点。
我需要将字典放入数组中,该怎么做,还是可以将每个值放入变量中,以便将它们打印到UITabDisplay?
    func makeGetCall() {
// Set up the URL request
let todoEndpoint: String = "https://api.jsecoin.com/v1.7/mining/auth/"

let apiKey = "xxxxxxxxxxxxxxxxxxxxxxx"


guard let url = URL(string: todoEndpoint) else {
print("Error: cannot create URL")
return
}
var urlRequest = URLRequest(url: url)

urlRequest.setValue(apiKey, forHTTPHeaderField: "Authorization")
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
// set up the session
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)

// make the request
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET on /todos/1")
print(error!)
return
}


// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
print("Got data: \(responseData)")
// parse the result as JSON, since that's what the API provides
do {
guard let todo = try JSONSerialization.jsonObject(with: responseData, options: [])
as? [String: Any] else {
print("error trying to convert data to JSON")
return
}
// now we have the todo
// let's just print it to prove we can access it
print("The todo is: " + todo.description)

// from this point on I"m experimenting ....

let dict = todo.description
print("The dict is: " + dict)

let keys = Array(todo.keys)
// prints ["mining", "success"]
print(keys)
/////////////////////////////////
if let alb = todo["mining"] {
// prints all the values for 'mining'.
// {
// 1532825277682891390 = {
// command = mining;
// siteid = "Platform Mining";
// ts = 1532825270557;
// user1 = 73276;
// value = 1;
// };
// ........ 30 entires
print(alb)
}
//======================
let index1 = todo.index(forKey: "mining")
let myMining = (todo[index1!].value)

// again prints all the mining values. Mining: {
// 1532825277682891390 = {
// command = mining;
// siteid = "Platform Mining";
// ts = 1532825270557;
// user1 = 73276;
// value = 1;
// };

print("Mining: \(myMining)")

//============

最佳答案

您可以采用与获取todo字典的键类似的方式进行操作。

if let mining = todo["mining"] as? [String: Any] {
let miningKeys = Array(mining.keys)
// It will be like [1532825277682891390, 1532826406100318457]
}

使用 miningKeys访问该挖掘词典中的值,例如:
mining[miningKeys.first!]
// It will return
// {
// command = mining;
// siteid = "Platform Mining";
// ts = 1532825270557;
// user1 = 73276;
// value = 1;
// }

关于ios - 如何从Swift 4/json字典中的多值键检索单个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51579146/

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