Adding [[String:AnyObject]] as parameter on Alamofire call (Swift 2) - json

I have this JSON (this is a simplified version):
{
"from":0,
"location": [{
"city":"Barcelona"
}]
}
And i'm trying to do an Alamofire request with this JSON on parameters. So I have typed something like this:
let parameter = [
"from":0,
"location": [
["city": "Barcelona"]
]
]
But it seems Alamofire doesn't accept the parse from [{ }] to [[]] on parameters. I have read that one way to solve this is using a MutableURLRequest but i don't find any clear example.
Alamofire.request(.GET, "http://myurl", parameters: (params as! [String : AnyObject]), headers: nil).responseCollection { (response: Response<[Group], NSError>) in
switch response.result {
case .Success(let groups):
print("Success")
case .Failure(_):
print("Failure")
}
}
This code above works and I get a result from this Alamofire Request but ignores this:
"location": [ ["city": "Barcelona"]]
Could someone can help with that? Thank you so much.

Related

SwiftUI: how to add type for JSON with 0 index array key

I am making a API call in swift and the response of the API looks like this:
[
{
"response": {
"data": {
"first": "Hello",
"second": "Hola",
"third": "Namaste"
}
}
}
]
Now to be able to integrate it into my app I need to decode the response from the API call for which the type I am using is as below:
struct APIResponse: Codable {
let response: Response
}
struct Response: Codable {
let data: Data
}
struct Data: Codable {
let first: String
let second: String
let third: String
}
Now the issue is this type is wrong because the API response is actually a array with only one element (index 0), which means the above type would work if the response from API was
[
"response": {
"data": {
"first": "Hello",
"second": "Hola",
"third": "Namaste"
}
}
]
how to define index-0 data in my type?
The code I am using to make API call and decode JSON:
var request = URLRequest(url: URL(string: "https://myurl.com")!)
let (data, _) = try await URLSession.shared.data(for: request)
let decodedResponse = try? JSONDecoder().decode(APIResponse.self, from: data)
Your response is an array of elements:
let decodedResponse = try JSONDecoder().decode([APIResponse].self, from: data)
and:
let element = decodedResponse.first
should get you going.
And never use try? it will obfuscate all errors.

Parsing JSON response in Swift 3

I've got an API endpoint that returns JSON in the following format:
[
{
"id": "1",
"name": "John"
},
{
"id": "2",
"name": "Jane"
},
{
"id": "3",
"name": "Nick"
}
]
I am trying to parse this in Swift 3, but I can only find examples to parse JSON formatted like so:
{
"blogs": [
{
"needspassword": true,
"id": 73,
"url": "http://remote.bloxus.com/",
"name": "Bloxus test"
},
{
"needspassword": false,
"id": 74,
"url": "http://flickrtest1.userland.com/",
"name": "Manila Test"
}
],
"stat": "ok"
}
, which has an extra level above what mine does.
So, where examples I've seen are simply parsing their data like jsonResponse["blogs"], I can't do that as my format is different.
How can I parse the format I've got, or how can I return a format that is easier to parse?
Any suggestions appreciated, thanks!
You can just do the following :
let data = // Data received from WS
do {
let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions()) as? [[String : String]]
//json is now an array from dictionary matching your model
}
catch {
//handle error
}
This will parse it when placed in the network call.
do {
let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions()) as! [[String : AnyObject]]
let firstPerson = json[0]
print(firstPerson)
let id = firstPerson["id"] as! String
print(id)
let name = firstPerson["name"] as! String
print(name)
} catch {
//handle error
}
Also, I tend to be against advising third party libraries, but SwiftyJSON is an exception I make. If you want to try it, add this to your pod file:
pod SwiftyJSON', '3.0.0'
Documentation: https://github.com/SwiftyJSON/SwiftyJSON
EDIT - Answering Comment:
Replacement line:
if let id = firstPerson["id"] as? String {
print(id)
}
Replacement line (if you need to hold on to the value):
var thisId: String?
if let id = firstPerson["id"] as? String {
thisId = id
}
print(thisId ?? "")
i don't really know swift but there might be the equivalent of ajax json encoding (server side you json_encode your array and client side you json_decode the response)
the idea is to have the same formatter that encodes and decodes the data

Alamofire POST JSON ARRAY

Alamofire - SWIFT JSON ARRAY
I want to Pass JSON ARRAY like this -
[
{
"OrgId": 1001,
"ClassworkId": 999800580,
}, {
"OrgId": 1001,
"ClassworkId": 0,
}
]
I am Using this Method - I want to solve parameter - [String : AnyObject] -> Array
func delateClasswork (parameters: [String: AnyObject],completion: (success : Bool) -> Void) {
request(.POST, "strURL", parameters: parameters, encoding:.JSON).responseJSON {
response in switch response.result {
case .Success(let JSON):
if((JSON.valueForKey("StatusId")) as! NSNumber == 1){
completion(success: true)
break
}else{
completion(success: true)
break
}
case .Failure(let error):
completion(success : false)
break
}
}
}
Assign the whole data to one parameter data, this will maintain the Type to [String:AnyObject].
let parameters:[String:AnyObject] = [
"data" : [
[
"OrgId": 1001,
"ClassworkId": 999800580,
],
[
"OrgId": 1001,
"ClassworkId": 0,
]
]
]
At the server end you have to parse the data using data key.

(Swift) twilio post request to set attributes using alamofire

I am attempting to use Twilio's REST Api and Alamofire to set certain Attributes to a Channel when creating it (https://www.twilio.com/docs/api/ip-messaging/rest/channels#action-create)
let parameters : [String : AnyObject] = [
"FriendlyName": "foo",
"UniqueName": "bar",
"Attributes": [
"foo": "bar",
"bar": "foo"
],
"Type": "private"
]
Alamofire.request(.POST, "https://ip-messaging.twilio.com/v1/Services/\(instanceSID)/Channels", parameters: parameters)
.authenticate(user: user, password: password)
.responseJSON { response in
let response = String(response.result.value)
print(response)
}
Using that code, the response I received back was that a Channel was created with FriendlyName foo and UniqueName bar, but that Channel had no Attributes set.
Looking at the Alamofire github (https://github.com/Alamofire/Alamofire) I see that there's a way to send a POST Request with JSON-encoded Parameters. So I tried this:
let parameters : [String : AnyObject] = [
"FriendlyName": "foo",
"UniqueName": "bar15",
"Attributes": [
"foo": "bar",
"bar": "foo"
],
"Type": "private"
]
Alamofire.request(.POST, "https://ip-messaging.twilio.com/v1/Services/\(instanceSID)/Channels", parameters: parameters, encoding: .JSON)
.authenticate(user: user, password: password)
.responseJSON { response in
let response = String(response.result.value)
print(response)
}
When adding "encoding: .JSON" to the request the response shows that not only were the Attributes not set but also the FriendlyName and UniqueName were nil, unlike before when they were correctly set using the URL-Encoded Parameters.
Am I setting the Attributes wrong in 'parameters'? Twilio's documentation says that Attributes is "An optional metadata field you can use to store any data you wish. No processing or validation is done on this field."
Help would be appreciated :)
I have figured out an answer to my question. Turns out I had formatted the Attributes field incorrectly.
Here is the code that worked for me:
let parameters : [String : AnyObject] = [
"FriendlyName": "foo",
"UniqueName": "bar",
"Attributes": "{\"key\":\"value\",\"foo\":\"bar\"}",
"Type": "private"
]
Alamofire.request(.POST, "https://ip-messaging.twilio.com/v1/Services/\(instanceSID)/Channels/", parameters: parameters)
.authenticate(user: user, password: password)
.responseJSON { response in
let response = String(response.result.value)
print(response)
debugPrint(response)
}
Hope this helps others :)

How to parse JSON in Swift?

I have some JSON data that looks like this which I am trying to parse in Swift.
[
[
{
a: "1",
b: "2"
},
[
{
c: "3",
},
{
d: "4",
}
]
]
]
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
if let myArray = json[0] as? [[AnyObject]] {
for myObject in myArray {
print("This works!\(myObject)")
}
}
However nothing I try seems to work - any help would be appreciated.
you can use SwiftyJSON - https://github.com/SwiftyJSON/SwiftyJSON
or create a class based on your JSON scheme try to parse with it.
like:
class object
{
let data = Array<subObject>()
}
class subObject
{
let subData = Array<Dictionary<AnyObject,AnyObject>>()
}
This snippet is not JSON. If it was JSON, the keys would be strings, like this:
[
[
{
"a": "1",
"b": "2"
},
[
{
"c": "3",
},
{
"d": "4",
}
]
]
]
And anyway in your screenshot we see that your JSON has already been parsed!
What you show in the image is not JSON either, but an array containing arrays and dictionaries...
But let's say your JSON is actually valid and the missing quotes are just a copy/paste problem.
Then to achieve your goal you have to cast the result of NSJSONSerialization to the correct JSON format, then you can access the inner objects.
Like this, for example:
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [[AnyObject]] {
if let myArray = json.first {
for myObject in myArray {
print("This works!\(myObject)")
}
}
}
} catch let error as NSError {
print(error.localizedDescription)
}