2017-05-05 72 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)