2015-05-25 32 views
0

在這裏我試圖使用移動號碼註冊應用程序,但我不能以正確的格式進行操作,我在Json解析註冊應用程序時出錯。在iOS中快速Json解析如何在post方法中傳遞參數

這裏,我給我所嘗試過的代碼,

var request = NSMutableURLRequest(URL: NSURL(string: "http://app.mycompany.in/gcm/test_slim.php/register")!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 5) 

var response: NSURLResponse? 
var error: NSError? 
var reponseError: NSError? 

var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError) 

// create some JSON data and configure the request 

let jsonString = "json=[{\"gsm_number\":\(Mobile),\"name\":\(Name),\"email\":\(Email),\"status\":\(Status),\"ver_code\":,\"profile_picture\":,\"device_id\":,\"gcm\":,\"is_register\":,\"thumb_image\":,\"user_baground_img\":}]" 

request.HTTPBody = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true) 
request.HTTPMethod = "POST" 
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") 

// send the request 
NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error) 

// look at the response 
if let httpResponse = response as? NSHTTPURLResponse { 
    println("HTTP response: \(httpResponse.statusCode)") 
    println("jsonString: \(jsonString)") 
    var responseData:NSString = NSString(data:urlData!, encoding:NSUTF8StringEncoding)! 

    let VerificationcodeViewController = self.storyboard?.instantiateViewControllerWithIdentifier("verificationcodeViewController") as UIViewController 

    self.navigationController?.pushViewController(VerificationcodeViewController, animated: true) 
} else { 
    println("No HTTP response") 
    var alertView:UIAlertView = UIAlertView() 
    alertView.title = "Error!" 
    alertView.message = "Error. & Some Problem was Found" 
    alertView.delegate = self 
    alertView.addButtonWithTitle("OK") 
    alertView.show() 
} 
+0

你可以在這裏發佈相關的錯誤? – Bannings

+0

請提及錯誤。 –

+0

語法錯誤發生 –

回答

1

你不能有空白鍵,試試這個:

let parameters = [ 
    "gsm_number" : Mobile, 
    "name" : Name, 
    "email" : Email, 
    "status" : Status, 
] 

let jsonData = NSJSONSerialization.dataWithJSONObject(parameters, options: NSJSONWritingOptions.allZeros, error: nil) 

let jsonString = "json=\(NSString(data: jsonData!, encoding: NSUTF8StringEncoding)!)" 
+0

謝謝,我會嘗試。 –

1

您jsonString不符合JSON語法。

  1. 你不能json=之初,=不是JSON允許的分隔符,使用:

  2. 你需要用雙引號括起來的變量(和轉義)

  3. 你不能有空白鍵,如認爲,只要值丟失,您必須使用null

爲您的變量有效的字符串的

例子:

let jsonString = "[{\"gsm_number\":\(Mobile),\"name\":\"\(Name)\",\"email\":\"\(Email)\",\"status\":\(Status),\"ver_code\":null}]" 

,或者在像你開始一個「JSON」鍵有:

let jsonString = "{\"json\":[{\"gsm_number\":\(Mobile),\"name\":\"\(Name)\",\"email\":\"\(Email)\",\"status\":\(Status),\"ver_code\":null}]}" 

在操場在將這個並顯示助理編輯「查看」 菜單中,它會幫助你理解:

let Mobile = 42 
let Name = "James" 
let Email = "[email protected]" 
let Status = 200 
let jsonString = "[{\"gsm_number\":\(Mobile),\"name\":\"\(Name)\",\"email\":\"\(Email)\",\"status\":\(Status),\"ver_code\":null}]" 
let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) 

斯威夫特1

var err: NSError? 
let json = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.allZeros, error: &err) as? [[String:AnyObject]] 
if err != nil { 
    println(err) 
} else { 
    println(json![0]["status"]!) 
} 

斯威夫特2

do { 
    if let data = data, 
     let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [[String:AnyObject]] { 
     if let status = json[0]["status"] as? Int { 
      print(status) 
     } 
    } 
} catch let error as NSError { 
    print(error.localizedDescription) 
} 
+0

謝謝,我會試試看。 –

相關問題