So the issue I am facing is that i have two arrays in an ios app which i need to store in a remote mySQL database, which i would like to accomplish using php and json. I haven't, however, despite several days of work, managed to get the arrays in the ios app converted into json code which doesn't crash the app. The arrays are populated by a qr code reader and input field, and there is always an equal amount of items in each array. Currently the code below generates the following json:
json string = {"b":"[\n\n]","p":"[\n\n]"}
No matter what changes i do, the app seems to crash with the following error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write', or alternatively it says Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} with some of my other experiments (like the version currently below)
var productArray = [String]()
var amountArray = [String]()
func addTapped(sender: UIBarButtonItem) {
print("Running add func")
do {
var test1 = ""
var test2 = ""
//Convert to Data
let jsonData1 = try! JSONSerialization.data(withJSONObject: amountArray, options: JSONSerialization.WritingOptions.prettyPrinted)
let jsonData2 = try! JSONSerialization.data(withJSONObject: productArray, options: JSONSerialization.WritingOptions.prettyPrinted)
//Convert back to string. Usually only do this for debugging
if let JSONString1 = String(data: jsonData1, encoding: String.Encoding.utf8) {
print(JSONString1)
test1 = JSONString1
}
if let JSONString2 = String(data: jsonData2, encoding: String.Encoding.utf8) {
print(JSONString2)
test2 = JSONString2
}
//In production, you usually want to try and cast as the root data structure. Here we are casting as a dictionary. If the root object is an array cast as [AnyObject].
var json1 = try JSONSerialization.jsonObject(with: jsonData1, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: AnyObject]
var json2 = try JSONSerialization.jsonObject(with: jsonData2, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: AnyObject]
let dict = ["json1": test1, "json2": test2] as [String: Any]
print("All JSON should print below")
print(dict)
if let jsonData = try? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted) {
let url = NSURL(string: "http://www.server.com/receiver")!
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request as URLRequest){ data,response,error in
if error != nil{
print(error?.localizedDescription)
return
}
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let resultValue:String = parseJSON["success"] as! String;
print("result: \(resultValue)")
print(parseJSON)
}
} catch let error as NSError {
print(error)
}
}
task.resume()
}
} catch {
print("Oops")
}
}