【问题标题】:passing JSON data with hebrew values problems (UTF8?)传递带有希伯来语值问题的 JSON 数据(UTF8?)
【发布时间】:2012-05-16 05:57:43
【问题描述】:

我有这个方法-

NSArray *objects = [NSArray arrayWithObjects:appDelegate.UserID,itemName.text,@"1",@"1",itemDegem.text,itemDescription.text, nil];
NSArray *keys = [NSArray arrayWithObjects:@"userId",@"itemName",@"itemCategoryId",@"regionId",@"degemName",@"itemDescription", nil];

NSDictionary *registerDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSString *jsonRequest = [registerDict JSONRepresentation];    
NSLog(@"jsonRequest is %@", jsonRequest);

NSURL *url = [NSURL URLWithString:@"http://www.somedomain.com/method"];

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

NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

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

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if (connection) {
    self.responseData = [NSMutableData data];
}

但是当我在我的对象中发送不是英语的字符串时,我从服务器收到错误。 现在,我知道这是一个 UTF8 问题,但我确实将它设置为 UTF8。有人可以帮我弄清楚吗?

【问题讨论】:

  • 您从服务器收到的错误是什么?你怎么知道 UTF-8 是问题所在?
  • 我得到一些 html 代码,基本上说:服务器在处理请求时遇到错误。异常消息是“格式化程序在尝试反序列化消息时抛出异常:尝试反序列化参数tempuri.org/:itemDescription 时出错。 InnerException 消息是“反序列化 System.String 类型的对象时出错。文件意外结束。以下元素未关闭:itemDescription、root.'。有关更多详细信息,请参阅 InnerException。有关详细信息,请参阅服务器日志

标签: objective-c json utf-8 sbjson


【解决方案1】:

我认为错误是strlen([jsonRequest UTF8String]) 不等于[jsonRequest length]

使用NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding]; 将一个NSString 转换为使用UTF8 编码的NSData。

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 2014-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多