Im trying to send a nested dictionary to an API. Even though none of the properties inside address is declared as optional, when I print this dictionary everything inside address is optional and the rest is non-optional. How to avoid this? Will creating a model and encoding that model to a dictionary result in the same behaviour?
var data = [
"individual": [
"address": [
"city": placemark.locality,
"country": placemark.isoCountryCode,
"line1": line1,
"line2": line2,
"postal_code": placemark.postalCode,
"state": placemark.administrativeArea
],
"dob": dob,
"email": email,
"first_name": firstName,
"last_name": lastName,
"phone": phone
],
"country": account_number.prefix(2),
"currency": "EUR",
"account_number": account_number
] as [String : Any]
Related
It appears there is still no way to import JSON or CSV files directly to a Firestore database. Many of the suggestions are for JavaScript-based apps that do not translate well to Swift. Does anyone have a good Swift solution for adding data to a Firestore database using JSON/CSV?
//example json
[
{
"name": "Stone Cove Marina Inc",
"email": "NOT IN SAMPLE",
"category": "Docks",
"category2": "Marinas",
"category3": "Dock Builders",
"address": "134 Salt Pond Rd",
"city": "Wakefield",
"state": "RI",
"zip": 2879,
"phone": "(401) 783-8990",
"website": "http://stonecovemarinari.com"
},
{
"name": "Bluehaven Homes",
"email": "NOT IN SAMPLE",
"category": "General Contractors",
"category2": "Home Builders",
"category3": "",
"address": "5701 Time Sq",
"city": "Amarillo",
"state": "TX",
"zip": 79119,
"phone": "(806) 452-2545",
"website": "http://www.bluehavenhomes.com/"
}
]
//here is the database structure
//collection is "businesses"; each "business" gets a document id; within each document id set the data
database.collection("businesses").document().setData(/*data here*/)
You can try
let str = """
[
{
"name": "Stone Cove Marina Inc",
"email": "NOT IN SAMPLE",
"category": "Docks",
"category2": "Marinas",
"category3": "Dock Builders",
"address": "134 Salt Pond Rd",
"city": "Wakefield",
"state": "RI",
"zip": 2879,
"phone": "(401) 783-8990",
"website": "http://stonecovemarinari.com"
},
{
"name": "Bluehaven Homes",
"email": "NOT IN SAMPLE",
"category": "General Contractors",
"category2": "Home Builders",
"category3": "",
"address": "5701 Time Sq",
"city": "Amarillo",
"state": "TX",
"zip": 79119,
"phone": "(806) 452-2545",
"website": "http://www.bluehavenhomes.com/"
}
]
"""
do {
let json = try JSONSerialization.jsonObject(with:str.data(using:.utf8)!, options: []) as! [[String: Any]]
for var i in 0...json.count - 1
{
database.collection("businesses").document().setData(json[i])
}
} catch {
print(error)
}
I am new to OData and I have a scenario to search for field name that contains words I am looking for. Below is the data I would like to search for the field name that ends with _isApproved. I am not sure if it possible to do such search.
[
{
"name": "John",
"address": "123 Example Dr.",
"city": "Frederick",
"zipcode": 654334,
"extension_345435423_isApproved": true
},
{
"name": "Smith",
"address": "124 Example Dr.",
"city": "Frederick",
"zipcode": 654374
}
]
I am not getting how to POST this type of data into webservice
{
"customer_id":"",
"customer_message": " entered by user",
"discount_amount": "",
"ip_address":"1.0.10.22",
"billing_address": {
"first_name": "hello",
"last_name": "world",
"company": "",
"street_1": "45 W test",
"street_2": "",
"city": "London",
"state": "Texas",
"zip": "123456",
"country": "United States",
"country_iso2": "US",
"phone": "",
"email": "xyz#example.com"
},
"shipping_addresses": [
{
"first_name": "rest",
"last_name": "Mctest",
"company": "Test Address",
"street_1": "rest test",
"street_2": "",
"city": "test",
"state": "test",
"zip": "12345",
"country": "United States",
"country_iso2": "US",
"phone": "",
"email": "xyzer#example.com"
}
],
"products": [
{
"product_id": 5448,
"quantity": 2
}
]
I am not getting how to POST this type of data into webservice.
Please help me
i am doing this type of data now i have to post this type. Can anyone post this type.
let aParam = ["email": Email, "password":Password] as [String:Any]
You are taking parameters as [String: Any], where Any denotes each type of data types. Say String, Array and Dictionary.
For example,
let aParam: [String: Any] = ["customer_id": "",
"customer_message": "",
"billing_address": ["first_name" : "hello",
"last_name" : "world",
"company" : "",
"street_1" : "45 W test"],
"shipping_addresses": [
["first_name" : "hello",
"last_name" : "world",
"company" : "",
"street_1" : "45 W test"]
],
"products": [
["product_id" : 5448,
"quantity" : 2],
["product_id" : 5450,
"quantity" : 1]
]
]
And complex json structure would be handle with content-type as JSON, so:
You just need to update your header as:
let aHeader = ["Content-Type" : "application/json"]
Hope this will help
I am trying to create an order from iOS to woo commerce using Alamofire. I am searching for a proper solution.
After trying to create an order I get this error:
{
code = "woocommerce_rest_cannot_create";
data = {
status = 401;
};
message = "Sorry, you are not allowed to create resources.";
}
Code:
let parameters: [String: AnyObject] = [
"consumer_key":"*******" as AnyObject, // here is my user name
"consumer_secret":"*******" as AnyObject, // here is my secret key
"shipping_total": "120.00" as AnyObject,
"total": "6015.00" as AnyObject,
"customer_id": 0 as AnyObject,
"billing": [
"first_name": "Faizul",
"last_name": "karim",
"company": "somecompany",
"address_1": "someAddress",
"address_2": "someAddress",
"city": "Dhaka",
"state": "Dhaka",
"postcode": "1203",
"country": "bd",
"email": "faizulkarim28#gmail.com",
"phone": "001929838939"
] as AnyObject,
"shipping": [
"first_name": "Faizul",
"last_name": "karim",
"company": "somecompany",
"address_1": "someAddress",
"address_2": "someAddress",
"city": "Dhaka",
"state": "Dhaka",
"postcode": "1203",
"country": "bd",
] as AnyObject,
"line_items":[
"id": 388,
"name": "Mens Casual Blazer - 40",
"product_id": 55677,
"variation_id": 57619,
"quantity": 1,
"tax_class": "",
"subtotal": "5895.00",
"subtotal_tax": "0.00",
"total": "5895.00",
"total_tax": "0.00",
] as AnyObject
]
Alamofire.request("https://infinitymegamall.com/wp-json/wc/v2/orders",method: .post, parameters: parameters)
.responseJSON{ response in
if let json = response.result.value {
print(json)
}
}
Are you sure your API Keys have write permissions? If GET works, it's possible that the key allows read only. Double check the permissions in your Woocommerce API Settings.
I want to create a dictionary with the next structure, for sending it via Alamofire as JSON to a server:
{
"user": {
"firstName": "fName",
"lastName": null,
"img": null,
"books": [
{
"id": 1
}, {
"id": 2
}
]
},
"locale": "en",
"gender": "male"
}
For this structure of JSON, I've tried the next:
let parameters: [[String: [String: String]], [String: String]]
but there a lot closures, so I confused with it. Can you help me with creating this structure?
The collection types in the Swift standard library only support homogenous collections, i.e. all elements in a collection must have the same type. So you cannot declare an array whose first element is of type [String: [String: String]] and whose second element is of type [String: String] if that is what you wanted.
This works:
let parameters = [
"user": [
"firstName": "fName",
"lastName": NSNull(),
"img": NSNull(),
"books": [
[ "id": 1],
[ "id": 2]
]
],
"locale": "en",
"gender": "male"
]
The type of parameters is [String: NSObject] where NSObject is the superclass of all the values in the dictionary. Note the use of NSNull to model null values, which is what NSJSONSerialization expects.