Question

I'm using NSJSONSerialization to parse JSON pulled down from a PHP page. The following code is supposed to parse the JSON and show the value of the data field (JSON sample included first):

{"status":200,"status_message":"Book Found","data":348}

code:

- (IBAction)btnClick:(id)sender
{
    if(![_txtBox.text isEqual:@""])
    {
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.mysite.comm/project/ws/name/%@",@"c"]]; //url for JSON

        NSError *jsonError=nil;

        NSString *jsonString = [self jsonStringWithUrl:url]; //get data as JSON

        NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; //convert to data

        NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error: &jsonError];

        _txtView.text = (NSString *)[jsonObject objectForKey:@"data"];
    }
}

However, the code crashes and I get the following error messages. I suspect it has something to do with this line: _txtView.text = (NSString *)[jsonObject objectForKey:@"data"]; but I'm not really sure what to look for. Am I trying to do get the data from the NSDictionary the wrong way?

2014-03-23 22:04:35.292 WebServiceTest[38975:60b] -[__NSCFNumber length]: unrecognized selector sent to instance 0x8d7f140
2014-03-23 22:04:35.296 WebServiceTest[38975:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x8d7f140'
*** First throw call stack:
(
    0   CoreFoundation                      0x017ec1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0156b8e5 objc_exception_throw + 44
    2   CoreFoundation                      0x01889243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x017dc50b ___forwarding___ + 1019
    4   CoreFoundation                      0x017dc0ee _CF_forwarding_prep_0 + 14
    5   Foundation                          0x011894e4 -[NSConcreteAttributedString length] + 42
    6   Foundation                          0x01188a6c -[NSConcreteAttributedString initWithString:attributes:] + 182
    7   UIKit                               0x00939e9d -[UITextView setText:] + 125
    8   WebServiceTest                      0x00002922 -[WebServiceTestViewController btnClick:] + 610
    9   libobjc.A.dylib                     0x0157d880 -[NSObject performSelector:withObject:withObject:] + 77
    10  UIKit                               0x0022d3b9 -[UIApplication sendAction:to:from:forEvent:] + 108
    11  UIKit                               0x0022d345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    12  UIKit                               0x0032ebd1 -[UIControl sendAction:to:forEvent:] + 66
    13  UIKit                               0x0032efc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
    14  UIKit                               0x0032e243 -[UIControl touchesEnded:withEvent:] + 641
    15  UIKit                               0x0026cddd -[UIWindow _sendTouchesForEvent:] + 852
    16  UIKit                               0x0026d9d1 -[UIWindow sendEvent:] + 1117
    17  UIKit                               0x0023f5f2 -[UIApplication sendEvent:] + 242
    18  UIKit                               0x00229353 _UIApplicationHandleEventQueue + 11455
    19  CoreFoundation                      0x0177577f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    20  CoreFoundation                      0x0177510b __CFRunLoopDoSources0 + 235
    21  CoreFoundation                      0x017921ae __CFRunLoopRun + 910
    22  CoreFoundation                      0x017919d3 CFRunLoopRunSpecific + 467
    23  CoreFoundation                      0x017917eb CFRunLoopRunInMode + 123
    24  GraphicsServices                    0x037e05ee GSEventRunModal + 192
    25  GraphicsServices                    0x037e042b GSEventRun + 104
    26  UIKit                               0x0022bf9b UIApplicationMain + 1225
    27  WebServiceTest                      0x000022fd main + 141
    28  libdyld.dylib                       0x01e33701 start + 1
    29  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
Was it helpful?

Solution

The JSON object you've shown has 348 listed as an integer value. So when NSJSONSerialization gets its hands on it, it converts it to an NSNumber. You'll need to call -[NSNumber stringValue] on [jsonObject objectForKey:@"data"] to convert the NSNumber to an NSString.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top