Ios NSJSONSerialization后无法读取数据
我正在从服务器获取一个json数组。jsonOutput对象正确显示了2个对象。但我无法显示或提取数据。谁能帮我一下吗。我试过了 方法如下:Ios NSJSONSerialization后无法读取数据,ios,objective-c,nsdictionary,nsjsonserialization,Ios,Objective C,Nsdictionary,Nsjsonserialization,我正在从服务器获取一个json数组。jsonOutput对象正确显示了2个对象。但我无法显示或提取数据。谁能帮我一下吗。我试过了 方法如下: for (id key in jsonOutput) { NSLog(@"key: %@, value: %@", key, [jsonOutput objectForKey:key]); } 声明: NSDictionary*jsonOutput 实际方法: - (void)connection:(NSURLCo
for (id key in jsonOutput) {
NSLog(@"key: %@, value: %@", key, [jsonOutput objectForKey:key]);
}
声明:
NSDictionary*jsonOutput
实际方法:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data=[[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
[data appendData:theData];
// if ([connection isEquals:connect1]){
// this is request urlConnectionRecsender
// }
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
jsonOutput= [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
for (id key in jsonOutput) {
NSLog(@"key: %@, value: %@", key, [jsonOutput objectForKey:key]);
}
}
试试这个
n错误*错误
jsonOutput= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if(error)
NSLog(@"%@",error.description);
如果您不知道什么是响应类型,那么最好先检查响应类型
id responseObj = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error];
if ([responseObj isKindOfClass:[NSArray class]]) {
//Your response is a array
}
else if([responseObj isKindOfClass:[NSDictionary class]]) {
//Your response is a dictionary
}
您的数组包含NSDictionary
{ cropName = corn; cropOrderId = 1; cropPrice = 100; "farmer_id" = 1; orderStatus = pending; quantity = 5; }
使用此代码获取值
for(NSDictionary*dict in jsonObject) {
NSArray *allKeysarr = [dic allKeys];
for(NSString *key in allKeysarr) {
NSLog(@"%@",[dic valueForKey:key]);
}
}
首次使用错误处理:
NSError *error;
jsonOutput= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error) NSLog(@"error: %@",error.description);
然后使用以下命令访问值:
NSString *value1 = [jsonOutput objectForKey:@"YOUR_KEY1"];
NSData*xmlData=[NSData dataWithContentsOfFile:xmlPath]
NSError *jsonError = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:xmlData options:kNilOptions error:&jsonError];
if ([jsonObject isKindOfClass:[NSArray class]]) {
NSLog(@"its an array!");
NSArray *jsonArray = (NSArray *)jsonObject;
NSLog(@"jsonArray - %@",jsonArray);
}
else {
NSLog(@"its probably a dictionary");
NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
NSLog(@"jsonDictionary - %@",jsonDictionary);
}
请尝试此操作一次否它不起作用相同的错误r-[\u NSCFArray objectForKey:]:当您响应为数组时,将无法识别的选择器发送到实例0x7518AA0。使jsonOutput到NSArrayI使用上述代码及其数组。但即使将类型更改为数组,我仍然会收到错误。使用for(jsonOutput中的id key){NSLog(key);}如何从数组中访问值?获取了当前用于(jsonOutput中的id key){NSLog(@“%@”,key);}的值。。。。作为{cropName=corn;cropOrderId=1;croprice=100;“farmer_id”=1;orderStatus=pending;quantity=5;}如何获取它们中的每一个?您可以传入一个错误变量并检查该对象。如果变量中没有写入错误,请检查是否有数组或字典。