I am trying to parse a JSON object with the below code:
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:_responseData options:NSJSONReadingMutableLeaves error:&myError];
NSArray *test = [res allValues];
@NSLog(@"%@", test);
I keep getting this error though I know the values look like the below:
2014-07-25 22:31:17.652 Application[18009:60b] (
{
"at_tag" = sam;
category = 1;
distance = "95.60";
"hash_tag" = idk;
uid = 1;
vidURI = "http://server/userPosts/avfgt123.mp4";
},
{
"at_tag" = nick;
category = 2;
distance = "99.45";
"hash_tag" = irdk;
uid = 2;
vidURI = "http://server/userPosts/avfg2223.mp4";
}
)
The whole error i get is this:
2014-07-25 22:33:13.829 App[18025:60b] -[__NSCFArray allValues]: unrecognized selector sent to instance 0x10d90c0d0
2014-07-25 22:33:13.831 App[18025:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray allValues]: unrecognized selector sent to instance 0x10d90c0d0'
*** First throw call stack:
(
0 CoreFoundation 0x0000000102477495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001021d699e objc_exception_throw + 43
2 CoreFoundation 0x000000010250865d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000102468d8d ___forwarding___ + 973
4 CoreFoundation 0x0000000102468938 _CF_forwarding_prep_0 + 120
5 App 0x00000001000028a7 -[BFTMainViewController connectionDidFinishLoading:] + 263
6 Foundation 0x0000000101ec736b __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 48
7 Foundation 0x0000000101d7abdb -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 210
8 Foundation 0x0000000101d7aaec -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 69
9 CFNetwork 0x0000000103289637 ___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke + 107
10 CFNetwork 0x0000000103287802 ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 84
11 CoreFoundation 0x000000010241df74 CFArrayApplyFunction + 68
12 CFNetwork 0x00000001031fa3e7 _ZN19RunloopBlockContext7performEv + 133
13 CFNetwork 0x00000001031fa217 _ZN17MultiplexerSource7performEv + 247
14 CFNetwork 0x00000001031fa03a _ZN17MultiplexerSource8_performEPv + 72
15 CoreFoundation 0x0000000102406d21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
16 CoreFoundation 0x00000001024065f2 __CFRunLoopDoSources0 + 242
17 CoreFoundation 0x000000010242246f __CFRunLoopRun + 767
18 CoreFoundation 0x0000000102421d83 CFRunLoopRunSpecific + 467
19 GraphicsServices 0x0000000103cf2f04 GSEventRunModal + 161
20 UIKit 0x0000000100d83e33 UIApplicationMain + 1010
21 App 0x0000000100001693 main + 115
22 libdyld.dylib 0x00000001039745fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I receive the error on line:
NSArray *test = [res allValues];
This has stumped me now for quite some time, I feel it is something simple I just have never ran into this issue before and can't put my finger on it.
As my comment above suggested: Your question shows that your JSON object is actually an NSArray
of NSDictionaries
(I think based on the Console output, If I'm wrong let me know though), so the allValues
method won't work, since the root object isn't an NSDictionary
. Aka: No visible @interface for NSArray declares the selector allValues
Try this:
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_responseData options:NSJSONReadingMutableLeaves error:&myError];
NSMutableArray *mutableArray = [[NSMutableArray alloc]init];
for (NSDictionary *sub in jsonArray)
{
[mutableArray addObjectsFromArray:[sub allValues]];
}
NSLog(@"Array is: %@",mutableArray);