2017-05-05 31 views
1

因此,我面临的问题是,我需要在远程mySQL数据库中存储两个数组,我需要使用php和json来完成ios应用程序。然而,尽管工作了几天,但我还没有设法将ios应用程序中的数组转换成json代码,这不会导致应用程序崩溃。数组由qr代码阅读器和输入字段填充,每个数组中总是有相等数量的项目。目前,下面的代码生成以下JSON:将swift数组解析为有效的json

json string = {"b":"[\n\n]","p":"[\n\n]"}

不管我做什么样的变化,应用程序似乎并出现以下错误崩溃: 终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因是:“* ** + [NSJSONSerialization dataWithJSONObject:options:error:]:JSON写入中无效的顶级类型',或者它表示错误域= NSCocoaErrorDomain Code = 3840“JSON文本没有以数组或对象开始,组。”的UserInfo =我的一些其他实验(如版本目前下文)

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") 
    } 

} 
+0

其中一行代码停止? – KKRocks

+0

@KKRocks这是我的问题的一部分 - XCode认为,尽管有僵尸对象启用,信息是不必要的,所以它没有告诉我这一点。 –

+0

设置异常断点并再次运行代码:http://stackoverflow.com/a/17802868/3901620 – KKRocks

回答

1

我认为最好的办法是使用{NSDebugDescription = JSON文字不与数组或对象,并选择允许片段未设置。开始} SwiftyJSON

白衣的是,如果你有一个字符串,你可以使用var json = JSON.parse(yourString)