【问题标题】:Getting Json Failed error while values of json data are posting to .net web service?当 json 数据的值发布到 .net Web 服务时出现 Json Failed 错误?
【发布时间】:2012-10-10 22:49:52
【问题描述】:

我将 json 数据发布到 .net 网络服务。我正在使用下面的代码。在检查 web 服务时它显示 MessageValue:Failed。

-(void)sentSignUpDetails { NSLog(@"sentSignUpDetails.......");

NSString *jsonRequest = [NSString stringWithFormat:@"{\"UserName\":\"%@\",\"Password\":\"%@\",\"DeviceToken\":\"%@\"}",self.emailtextfield.text,self.passwordtextfield.text,appDelegate.Devicetoken];

NSLog(@"jsonRequest is %@", jsonRequest);
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://brewedlife.testshell.net/Brewedlife/AppSignup"]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

NSLog(@"1239");
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

con = [NSURLConnection connectionWithRequest:request delegate:self];

}

  • (void) 连接:(NSURLConnection *)connection didReceiveData:(NSData *)data1

{

 if (!data) 
  {
    data = [[NSMutableData alloc] initWithData:data1];
  }

 else 
  {
    [data appendData:data1];
  }

} 它得到 json failed 错误,如下所示 -JSONValue failed。错误跟踪是:( "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Unrecognizedleading character\" UserInfo=0x15e880 {NSLocalizedDescription=Unrecognizedleading character} 请给出任何想法。谢谢

【问题讨论】:

    标签: iphone objective-c ios xcode


    【解决方案1】:

    像这样创建您的 JSON 请求

    NSDictionary *jsonDict = [NSDictionary 字典WithObjectsAndKeys: @"ValueOfUsername", @"keyOfUserName", @"ValueOfPassword", @"Password", @"ValueOfDeviceToken", @"DeviceToken",nil];
    NSString *req = [jsonDict JSONRepresentation];

    如果您收到编码错误,请查看这是否有效 NSUTF8StringEncoding

    【讨论】:

      猜你喜欢
      • 2016-03-23
      • 2023-03-21
      • 2016-12-27
      • 1970-01-01
      • 2015-12-17
      • 2015-04-10
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      相关资源
      最近更新 更多