【问题标题】:JSON parsing is sending error message in iOS 7.1JSON 解析在 iOS 7.1 中发送错误消息
【发布时间】:2014-12-31 06:05:41
【问题描述】:

我检查了我的代码,但没有发现我的代码有什么问题..我正在使用 post 方法进行 JSON 解析。同样的代码在 Xcode 5 中工作,但在 Xcode 6 中不起作用。在我的JSON序列化。

 parsingResultLogin = {
   "error_code" = "-1";
   "error_message" = "";
  }

我的代码是 -

-(void)loginFromServer
{
   NSString *strURL = [NSString stringWithFormat:@"%@login",GLOBALURLDOMAIN];
   NSLog(@"strURL =%@",strURL);
   NSData *dataPostLogin = nil;
   NSDictionary *dicPostDataLogin = [ NSDictionary dictionaryWithObjectsAndKeys:@"qwertyuiopwqq",@"username",@"qwertyuiop",@"password",@"1234567890987654",@"device_token",@"ios",@"device_type", nil];
   NSLog(@"%@",[dicPostDataLogin description]);
   dataPostLogin = [NSJSONSerialization dataWithJSONObject:dicPostDataLogin options:NSJSONWritingPrettyPrinted error:nil];
   NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
   NSLog(@"request = %@",request);
   [request setHTTPBody:dataPostLogin];
   [request setHTTPMethod:@"POST"];
   [request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[dataPostLogin length]] forHTTPHeaderField:@"Content-Length"];
   [request setValue:@"JSON/application" forHTTPHeaderField:@"Content-Type"];
   NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
   NSLog(@"responsedata =%@",responseData);
   if (responseData == NULL) {
      AppDelegate *appdel = [[UIApplication sharedApplication]delegate];
      [appdel alertError];
   }
   else
   {
       NSDictionary *parsingResultLogin = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
    NSLog(@"parsingResultLogin = %@",parsingResultLogin);
    //NSString *strParseDataResult = [parsingResultLogin objectForKey:@""];
   }
}

【问题讨论】:

    标签: objective-c ios7 xcode5 xcode6


    【解决方案1】:

    JSON 中的键/值由“:”分隔,而不是“=”。最后不应该有分号。所以这不是有效的 JSON,也不会使用 JSON 解析器进行解析。

    【讨论】:

    • 你能告诉我如何在上面的代码中将“=”更改为“:”。
    • :我添加了“NSString *jsonString = [[NSString alloc] initWithData:dataPostLogin encoding:NSUTF8StringEncoding];”并传递给“ [request setHTTPBody:jsonString];”但收到警告,因为它不是 nsdata 类型。
    • PHP web服务有什么需要改的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多