码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发——错误总结篇&开发中常见错误和警告总结(十九)

时间:2015-08-27 21:06:42      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

开发中常见错误和警告总结(十九)

]NSJSONSerialization 解析错误既不是字典,也不是数组

 

最近在 实现JSON解析的时候遇到一个很经典的错误,找了好久都没有找到好的,方法,后面网上看了一下,发现自己脑子被门给挤了!

 

我成功地从我的服务器中获取数据。和拿到它后我的数据发送到函数来分析 ;

 1 - (void)readIn:(NSMutableData *)s {
 2     NSLog(@"Reading in the following:");
 3     NSString * prints = [[NSString alloc] initWithData:s  encoding:NSUTF8StringEncoding];
 4     NSLog(@"%@", prints);
 5 
 6     NSError *error = nil;
 7     NSData *jsonData = [[NSData alloc] initWithData:s];
 8 
 9     if (jsonData) {
10 
11         id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
12 
13         if ([jsonObjects isKindOfClass: [NSArray class]])
14             NSLog(@"yes we got an Array");
15         else if ([jsonObjects isKindOfClass: [NSDictionary class]])
16              NSLog(@"yes we got an dictionary");
17         else
18               NSLog(@"neither array nor dictionary!");
19 
20 
21 
22         if (error) {
23             NSLog(@"error is %@", [error localizedDescription]);
24             return;
25         }
26 
27         NSArray *keys = [jsonObjects allKeys];
28         for (NSString *key in keys) {
29             NSLog(@"%@ is %@",key, [jsonObjects objectForKey:key]);
30         }
31 
32     } else {
33 
34         // Handle Error
35     } 
36     }

 

现在我在控制台上的打印的是:

  • 2012-08-17 13:59:57.667 TaraftarlikOyunu[1157:c07] Reading in the following: 2012-08-17 13:59:57.667 TaraftarlikOyunu[1157:c07] {"uID":"5878341","tm":"fb","hh":122,"pt":75,"coin":500,"ll":1,"qlevel":1,"coect":true,"potWeekly":{"pts":75,"intval":604800000},"acent":{"chamunt":0},"mes":[]} 2012-08-17 13:59:57.668 TaraftarlikOyunu[1157:c07] neither array nor dictionary! 2012-08-17 13:59:57.670 TaraftarlikOyunu[1157:c07] error is The operation couldn’t be completed. (Cocoa error 3840.)

看来法律 json 对象给我。我在哪里做错了?

我从 nsstream ; 与服务器上获取数据和在这里是我的代码来获取数据:

 1 case NSStreamEventHasBytesAvailable: {
 2             if(stream == inputStream) {
 3                 NSLog(@"inputStream is ready.");
 4 
 5                 uint8_t buf[1024];
 6                 unsigned int len = 0;
 7 
 8                 len = [inputStream read:buf maxLength:1024];
 9             NSLog(@"length %i", len);
10                 if(len > 0) {
11 
12                     NSMutableData* data=[[NSMutableData alloc] initWithLength:0];
13                     [data appendBytes: (const void *)buf length:len];
14                     [self readIn:data];
15 
16                 }
17             }
18             break;
19         }

 

解决方法 1:

问题是我得到的 json 字符串来与空终止在结束了,当我尝试反序列化时它可以不转换为 NSDictionary 或 NSArray。对代码做一点小变动使得一切都完美。真正的代码应该是这样

 1 case NSStreamEventHasBytesAvailable: {
 2         if(stream == inputStream) {
 3 
 4             NSLog(@"inputStream is ready.");
 5 
 6             uint8_t buf[1024];
 7             unsigned int len = 0;
 8 
 9             len = [inputStream read:buf maxLength:1024];
10             NSLog(@"length %i", len);
11             if(len > 0) {
12 
13                 datum =[[NSMutableData alloc] initWithLength:0];
14 
15                 [datum appendBytes: (const void *)buf length:len-1];
16 
17 
18 
19 
20                 NSDictionary * jsondict = [NSJSONSerialization JSONObjectWithData:datum options:NSUTF8StringEncoding error:nil];
21 
22                 NSLog(@"is valid json object %d",[NSJSONSerialization isValidJSONObject:jsondict]);
23 
24 
25                 [self readIn:datum];
26                 }
27             }
28              else {
29                 NSLog(@"no buffer!");
30             }
31             break;
32             }
33 
34 
35     default: {
36         NSLog(@"Stream is sending an Event: %i", event);
37 
38         break;
39     }
40 }

 

仅从另一个区别是我扔的最后一个字节,它成为了有效 json 字典。谢谢你是我的问题有兴趣的人。

iOS开发——错误总结篇&开发中常见错误和警告总结(十九)

标签:

原文地址:http://www.cnblogs.com/iCocos/p/4764439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!