简体   繁体   English

JSON Objective-C解析失败

[英]JSON Objective-C Parsing Fail

I have written the following code but I keep on getting nil. 我已经写了下面的代码,但我一直没有得到。 I have tried many different variations of this but I am failing exceptionally hard. 我已经尝试过许多不同的方法,但是我却异常失败。

This is what I am getting from the server. 这就是我从服务器上得到的。 Two objects. 两个对象。

[{"description":"yolo.","name":"ye","id":1},{"description":"sMITH","name":"John","id":2}] [{{“ description”:“ yolo。”,“ name”:“ ye”,“ id”:1},{“ description”:“ sMITH”,“ name”:“ John”,“ id”:2}]

Any help would be greatly appreciated...... Thanks. 任何帮助将不胜感激......谢谢。

    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    NSArray *jsonObjects = [jsonParser objectWithData:response];
    NSMutableString *yolo = [[NSMutableString alloc] init];

            for ( int i = 0; i < [jsonObjects count]; i++ ) {
                NSDictionary *jsonDict = [jsonObjects objectAtIndex:i];
                NSString *IDID = [jsonDict objectForKey:@"id"];
                NSString *name = [jsonDict objectForKey:@"name"];

                NSLog(@"ID: %@", IDID); // THIS DISPLAYS

                [yolo appendString: IDID];    // THIS seems to be causing the new error...
                [yolo appendString:@": "];
                [yolo appendString: name];
                NSLog(@"%@", yolo);           // RETURNS NIL    
    }

EDIT: 编辑:

currently my new error is... 目前我的新错误是...

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSDecimalNumber length]: unrecognized selector sent to instance 0x81b89f0' 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[NSDecimalNumber length]:无法识别的选择器已发送至实例0x81b89f0'

Looks like your [jsonDict objectForKey:@"id"] is an NSNumber (or NSDecimalNumber ) and not an NSString . 看起来您的[jsonDict objectForKey:@"id"]是一个NSNumber (或NSDecimalNumber ),而不是NSString You should change the line NSString *IDID = [jsonDict objectForKey:@"id"]; 您应该更改NSString *IDID = [jsonDict objectForKey:@"id"]; to, 至,

id myObject = [jsonDict objectForKey:@"id"];
NSString *IDID = nil;

if ([myObject isKindOfClass:[NSNumber class]]) {
   IDID = [[jsonDict objectForKey:@"id"] stringValue];
} else {
   IDID = [jsonDict objectForKey:@"id"];
}

This error appeared now since earlier you were not initializing NSMutableString *yolo and you were using appendString: on a nil object. 现在出现此错误,因为之前您没有初始化NSMutableString *yolo并且在nil对象上使用了appendString: Since now it is initialized as NSMutableString *yolo = [[NSMutableString alloc] init]; 从现在开始,它被初始化为NSMutableString *yolo = [[NSMutableString alloc] init]; it is trying to call appendString on NSMutableString object which accepts only NSString type as its inputs where as you are passing an NSNumber in it. 它试图在NSMutableString对象上调用appendString ,该对象仅接受NSString类型作为其输入,而您正在其中传递NSNumber length is a method which appendString: internally calls. lengthappendString:内部调用的方法。 So you need to change this as well. 因此,您也需要更改此设置。

You never initialize yolo , so it's just nil the whole time you're calling -appendString: on it. 您永远不会初始化yolo ,因此在您一直在其上调用-appendString:时它只是nil Try this: 尝试这个:

NSMutableString *yolo = [NSMutableString string];

您是否尝试过初始化NSMutableString?

NSMutableString *yolo = [[NSMutableString alloc] init];

It looks like you are not really checking the type of the data coming to your app via your JSON feed. 看来您并没有真正检查通过JSON feed进入应用程序的数据类型。 This might be the case of random crashes when users actually use your app. 当用户实际使用您的应用时,可能是随机崩溃的情况。 It might be also a reason for rejection to the App Store, is such crashes happen during your App's review. 也可能是拒绝App Store的原因,因为在您的App审核期间会发生此类崩溃。

You should be checking the type of all objects you receive from JSON, before calling methods on them :) 您应该检查从JSON接收到的所有对象的类型,然后再对它们调用方法:)

By implementing best practices you will have a stable and usable app. 通过实施最佳实践,您将拥有一个稳定且可用的应用程序。 Build data models to validate your data. 建立数据模型以验证您的数据。 You can also you a JSON data model framework like JSONModel: http://www.jsonmodel.com/ 您也可以像JSONModel这样的JSON数据模型框架: http : //www.jsonmodel.com/

It's obvious from your data that "id" is not a string, but a number. 从您的数据显而易见,“ id”不是字符串,而是数字。 Assigning a pointer to an NSString* doesn't magically convert it to an NSString*. 将指针分配给NSString *不会神奇地将其转换为NSString *。 And it's obvious from the exception that you got that some object is an NSDecimalNumber when you thought it would be an NSString. 从异常中可以明显看出,当您以为某个对象是NSString时,就会得到一个对象是NSDecimalNumber。

So: IDID is an NSNumber*, and pretending it is an NSString* will lead to crashes. 因此:IDID​​是一个NSNumber *,并且假装它是NSString *会导致崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM