NSDictionary在IOS/Objective c中显示nil
我正在通过web服务接收阿拉伯语数据。我以JSON对象的形式获取数据,但当我将值传递给NSDictionary在IOS/Objective c中显示nil,ios,objective-c,json,nsdictionary,Ios,Objective C,Json,Nsdictionary,我正在通过web服务接收阿拉伯语数据。我以JSON对象的形式获取数据,但当我将值传递给NSDictionary时,它会显示nil,并且无法获取key/pair值。我在data之前接收值,但我无法将其传递给NSDictionary 代码如下: (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSDictionary* json = [NSJSONSerialization
NSDictionary
时,它会显示nil
,并且无法获取key/pair
值。我在data
之前接收值,但我无法将其传递给NSDictionary
代码如下:
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
for (NSString *key in [json allKeys])
{
NSArray *feed = [json objectForKey:key];
for(int i=0; i<feed.count;i++)
{
for (NSString *key1 in [[feed objectAtIndex:i] allKeys])
{
if([key1 isEqualToString:@"newsTitle"])
{
NSString *value = [[feed objectAtIndex:i] objectForKey:key1];
[NewsTitleArray addObject:value];
}
else if([key1 isEqualToString:@"newsDescription"])
{
NSString *value = [[feed objectAtIndex:i] objectForKey:key1];
[NewsDescriptionArray addObject:value];
}}}}
(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据{
NSDictionary*json=[NSJSONSerialization JSONObject WithData:数据选项:kNilOptions错误:nil];
for(NSString*输入[json allKeys])
{
NSArray*feed=[json objectForKey:key];
对于(int i=0;i您需要为您的数据使用正确的编码,通常阿拉伯语内容需要ISO拉丁语
编码。虽然您的代码是错误的,但您需要使用正确的NSURLConnection委托函数来获取完整的数据,您正在尝试解析不完整的数据
请参见下面的更改
声明变量
NSMutableData *receivedData;
处理代理调用
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
//This delegate function gets called multiple times while fetching the data..
if(!receivedData){
receivedData=[[NSMutableData alloc] initWithData:data];
}else{
[receivedData appendData:data];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
//When the connection completes the processing, do you parsing
if(receivedData.length>0){
NSString *string = [[NSString alloc] initWithData:receivedData encoding:NSISOLatin1StringEncoding];
NSData *utf8Data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:utf8Data options:kNilOptions error:nil];
for (NSString *key in [json allKeys])
{
NSArray *feed = [json objectForKey:key];
for(int i=0; i<feed.count;i++)
{
for (NSString *key1 in [[feed objectAtIndex:i] allKeys])
{
if([key1 isEqualToString:@"newsTitle"])
{
NSString *value = [[feed objectAtIndex:i] objectForKey:key1];
[NewsTitleArray addObject:value];
}
else if([key1 isEqualToString:@"newsDescription"])
{
NSString *value = [[feed objectAtIndex:i] objectForKey:key1];
[NewsDescriptionArray addObject:value];
}
}
}
}
}
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据{
//此委托函数在获取数据时被多次调用。。
如果(!receivedData){
receivedData=[[NSMutableData alloc]initWithData:data];
}否则{
[接收数据附录数据:数据];
}
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接{
//当连接完成处理时,是否执行解析
如果(接收数据长度>0){
NSString*string=[[NSString alloc]initWithData:receivedData编码:NSISOLatin1StringEncoding];
NSData*utf8Data=[字符串数据使用编码:NSUTF8StringEncoding];
NSDictionary*json=[NSJSONSerialization JSONObject WithData:utf8Data选项:kNilOptions错误:nil];
for(NSString*输入[json allKeys])
{
NSArray*feed=[json objectForKey:key];
对于(int i=0;iIs您从服务器接收到的JSON值可能不是字典,或者可能包含其他字符,使其成为无效的JSON字符串?您是否也可以包括从服务器接收到的JSON字符串?@Chonch我使用jsonlint.com/…检查字符串,它说它是validate JSON。Thnx我可以使用相同的字符串吗英语代码?或者我需要更改编码:NSISOLatin1StringEncoding]
中的内容吗?我的应用程序以英语和阿拉伯语为基础well@jithinvarghese如果数据是两者的混合,请使用ISO,否则不需要转换,您可以直接使用它。@iPhone am使用两个web服务,一个用于阿拉伯语数据,另一个用于英语数据..,当我单击阿拉伯语按钮,它将调用阿拉伯语web服务,当我单击英语时,它将调用英语web服务,所以我的问题是,我可以为这两个使用相同的代码吗?谢谢you@jithinvarghese是的,我认为它应该可以工作,因为在英语中不会有任何字符需要转换为ISO拉丁语,因此不会发生转换,代码应该工作