iOS 7解析麻烦

时间:2014-06-03 21:15:01

标签: ios json

假设发送请求到以下地址:site_uri / user / key / datatype,带有以下json原始主体:

{"user": {
  "initials": "initials",
  "insertion": "",
  "name": "test name",
  "gender": "m",
  "dateofbirth": 123456789,
  "address": "test address",
  "number": "1",
  "zipcode": "4213AD",
  "city": "test city",
  "phone_home": "test",
  "phone_mobile": "",
  "email_addresses": {"email": [
    "jp@test.com",
    "jp2@test.com"
  ]}
}}

以及标题:Content-type:application / json,charset:utf-8; Content-Length:postData.length

post请求通过发送到服务器的json原始主体编辑用户实体的变量。在Xcode中,代码如下:

    -(BOOL)updateUserData:(User*)userEntity {    
    NSString* apiKey = [[NSUserDefaults standardUserDefaults] objectForKey:kRMS_API_KEY];
    NSString* urlStr = [NSString stringWithFormat:@"%@/user/%@/%@",kURL_BASE,apiKey,kURL_RESPONSE_JSON];
    NSMutableDictionary* userDict = [NSMutableDictionary dictionary];
    [userDict setObject:userEntity.initials forKey:@"initials"];
    [userDict setObject:userEntity.insertion forKey:@"insertion"];
    [userDict setObject:userEntity.name forKey:@"name"];
    [userDict setObject:userEntity.gender forKey:@"gender"];
    [userDict setObject:userEntity.dateofbirth forKey:@"dateofbirth"];
    [userDict setObject:userEntity.address forKey:@"address"];
    [userDict setObject:userEntity.number forKey:@"number"];
    [userDict setObject:userEntity.zipcode forKey:@"zipcode"];
    [userDict setObject:userEntity.city forKey:@"city"];
    [userDict setObject:[[[userEntity.phone_home stringByReplacingOccurrencesOfString:@","              withString:@""]stringByReplacingOccurrencesOfString:@" " withString:@""]stringByReplacingOccurrencesOfString:@"-" withString:@""] forKey:@"phone_home"];
    [userDict setObject:[[[userEntity.phone_mobile stringByReplacingOccurrencesOfString:@"," withString:@""]stringByReplacingOccurrencesOfString:@" " withString:@""]stringByReplacingOccurrencesOfString:@"-" withString:@""] forKey:@"phone_mobile"];
    [userDict setObject:userEntity.timestamp forKey:@"timestamp"];
    [userDict setObject:[NSDictionary dictionaryWithObject:userEntity.email_addresses forKey:@"email"] forKey:@"email_addresses"];    
    userDict = [NSMutableDictionary dictionaryWithObject:userDict forKey:@"user"];
    NSError* err = nil;
    NSData* postData = [[CJSONSerializer serializer] serializeDictionary:userDict  error:&err];
    if (err){
       DLog(@"error %@",err);
       return NO;
    }
    NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL  URLWithString:urlStr]                                                   cachePolicy:NSURLCacheStorageNotAllowed                                         timeoutInterval:10] autorelease];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"utf-8" forHTTPHeaderField:@"charset"];
    [request setValue:[NSString stringWithFormat:@"%d",postData.length] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];

因此,在iOS 5和6中,没有问题。问题出在iOS 7中:当我发送一个帖子请求时:

  {"user": {
    "initials": "initials",
    "insertion": "",
    "name": "test name",
    "gender": "m",
    "dateofbirth": ,
    "address": "test address",
    "number": "1",
    "zipcode": "",
    "city": "",
    "phone_home": "",
    "phone_mobile": "",
    "email_addresses": {"email": [
    "constant",
    ""
       ]}
     }}

,但我得到以下内容:

{"user": {
  "initials": "initials",
  "insertion": "",
  "name": "test address",
  "gender": "m",
  "dateofbirth": ,
  "address": "test address",
  "number": "",
  "zipcode": "",
  "city": "",
  "phone_home": "",
  "phone_mobile": "",
  "email_addresses": {"email": [
    "constant",
    ""
  ]}
}}

从调试器中我可以注意到,实际发生的是:以下键的值:“姓名缩写”,“地址”,“性别”,“插入”和“email_addresses”保持不变,但是“name”的值将替换为“address”键值,而上面未提及的键值(number,zip code,city,phone_home,phone_mobile)将在post请求后删除。这也发生在数据可以在post请求中发送到服务器之前。这只发生在iOS 7.0 / 7.1中,我不确定它是什么。 对这个家伙的任何建议/想法?抱歉我的信息不足,因为我现在是这个领域的新手。

0 个答案:

没有答案