how can I retrieve individual values from a multi value key in Swift 4/json dictionary?

Multi tool use


how can I retrieve individual values from a multi value key in Swift 4/json dictionary?
There are many many examples of retrieving the value of a key, and I can do that. In this case, the key has multiple values. The key is "mining", followed by 6 values. The same key:value is repeated 30 times, with different values for the keys of course.
What I can't seem to solve is how to return the values. Especially the first one, 1532825277682891390, for example, which appears to be a sub-key with key/value pairs related to that.
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;
};
....
....
....
}
I can show the key:value for the "mining" key. There are only two keys in the dictionary, 'mining' and 'success' - I'm not worried about 'success', it usually returns '1'.
This is basically the code with a few examples and trial outputs, as I try out different things from other posts. I just can't get my head around this bit.
Do I need to put the dictionary into an array, and how do I do that, or can I just put each value into a var, so I can print them to a 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)")
//============
1 Answer
1
You can do it in a similar way you have written for taking the keys of todo dictionary.
if let mining = todo["mining"] as? [String: Any] {
let miningKeys = Array(mining.keys)
// It will be like [1532825277682891390, 1532826406100318457]
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.