Monday, 21 September 2015

json string send to server

Objective c:

 NSString *jsonString;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:mutablearray
                                                       options:NSJSONWritingPrettyPrinted 
                                                         error:&error];
    if (!jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString  = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        

    }

eg:

NSDictionary  *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                // Object and key pairs
                firstName, @"fname",
                lastName, @"lname",
                phoneNumStr, @"phone_no",
                emailStr, @"email_id",
                nil];

        [allObjectArray addObject:dict];

php:

$json_str = $_POST['jsonstring'];

$contact_details = json_decode($json_str);
   
      foreach($contact_details as $key => $value)
      {
  $fname = $value->fname;
  $lname = $value->lname;
  $email = $value->email_id;
  $phone = $value->phone_no;

//insert or update query
}

No comments:

Post a Comment