0

Here I have Tried to Sign Up the App Using Mobile Number but I cant do in correct format,I have error in Json parsing for signup the app.

Here I give the code what i am tried,

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()
}
4
  • Can you post relevant error here? Commented May 25, 2015 at 11:01
  • Please mention the error. Commented May 25, 2015 at 11:22
  • syntax error was occur Commented May 25, 2015 at 11:22
  • I made a mistake in pass parameters. Commented May 25, 2015 at 11:22

2 Answers 2

1

You can't have empty keys, try this :

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)!)"
Sign up to request clarification or add additional context in comments.

Comments

1

You jsonString does not conform to the JSON syntax.

  1. You can't have json= at the beginning, = isn't an allowed separator in JSON, use :

  2. You need to wrap the string variables with double quotes (and escape them)

  3. You can't have empty keys like that with just the value missing, you have to use null

Example of valid string for your variables:

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

Or with a 'json' key at the beginning like you had:

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

Put this in a Playground and show the Assistant Editor in the "View" menu, it will help you understand:

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)

Swift 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"]!)
}

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.