When I build and run the following:
// Grabbing the Overlay Networks
let urlString = "https://test.sdnnw.net/networks/overlay_networks"
if let url = NSURL(string: urlString) {
let URLRequest = NSMutableURLRequest(URL: url)
URLRequest.setValue("token", forHTTPHeaderField: "User-Token")
URLRequest.setValue("username", forHTTPHeaderField: "User-Auth")
URLRequest.HTTPMethod = "GET"
Alamofire.request(URLRequest).responseJSON { (response) -> Void in
if let value = response.result.value {
let json = JSON(value)
print(json)
}
}
}
I get the following results (which is correct):
[
{
"uuid" : "c8bc05c5-f047-40f8-8cf5-1a5a22b55656",
"description" : "Auto_API_Overlay",
"name" : "Auto_API_Overlay",
}
]
When I build and run the following:
// Grabbing the Overlay Networks
let urlString = "https://test.sdnnw.net/networks/overlay_networks"
if let url = NSURL(string: urlString) {
let URLRequest = NSMutableURLRequest(URL: url)
URLRequest.setValue("token", forHTTPHeaderField: "User-Token")
URLRequest.setValue("username", forHTTPHeaderField: "User-Auth")
URLRequest.HTTPMethod = "GET"
Alamofire.request(URLRequest).responseJSON { (response) -> Void in
if let value = response.result.value {
let json = JSON(value)
print(json["name"].stringValue)
print(json["description"].stringValue)
print(json["uuid"].stringValue)
}
}
}
I get blank output - no null
, nil
, or [:]
, just blank. Scoured SwiftyJSON
and here and haven't found anything close to clearing up why stringValue
is not working correctly (maybe I'm searching using incorrect keywords?). Would very much appreciate some feedback as to what I'm doing incorrectly.
In JSON, the []
characters are for arrays, and {}
are for dictionaries.
Your JSON result:
[ { "uuid" : "c8bc05c5-f047-40f8-8cf5-1a5a22b55656", "description" : "Auto_API_Overlay", "name" : "Auto_API_Overlay", } ]
is an array containing a dictionary.
Get the contents with a loop, for example.
Using SwiftyJSON, loop with a tuple (first arg of a SwiftyJSON object is the index, second arg is the content):
for (_, dict) in json {
print(dict["name"].stringValue)
print(dict["description"].stringValue)
print(dict["uuid"].stringValue)
}
Be careful with SwiftyJSON properties ending with Value
as they are the non-optional getters (if the value is nil, it will crash). The optional getters don't have Value
:
for (_, dict) in json {
if let name = dict["name"].string,
desc = dict["description"].string,
uuid = dict["uuid"].string {
print(name)
print(desc)
print(uuid)
}
}