This question already has answers here:
How to parse received response [closed]
(6 answers)
Closed 9 years ago.
I was trying to get the JSON data to an array. Below is the code... I dont know where am I going wrong..
When I keep a Log for arrayJSON Below is the result :
Array Result: [{"ID_PROJECT":1,"NM_PROJECT":"TED"},{"ID_PROJECT":2,"NM_PROJECT":"DL"},{"ID_PROJECT":3,"NM_PROJECT":"PERD"},{"ID_PROJECT":4,"NM_PROJECT":"EPRS "},{"ID_PROJECT":5,"NM_PROJECT":"MSCS"}]
NSArray *arrayJSON = [NSJSONSerialization JSONObjectWithData:urlData
options:NSJSONReadingAllowFragments
error:&error];
NSMutableArray *arrayResult = [NSMutableArray array];
for(NSDictionary *dictJSON in arrayJSON){
[arrayResult addObject: [dictJSON objectForKey:#"NM_PROJECT"]];
}
NSLog(#"Array Result: %#", arrayResult);
Here is the error code :
014-02-25 15:07:40.602 Demologin[1146:c07] * Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFString countByEnumeratingWithState:objects:count:]:
unrecognized selector sent to instance 0x715f430'
* First throw call stack: (0x1ca3012 0x114fe7e 0x1d2e4bd 0x1c92bbc 0x1c9294e 0xad28 0x1061c7 0x106232 0x553d5 0x5576f 0x55905 0x5e917
0x2296c 0x2394b 0x34cb5 0x35beb 0x27698 0x1bfedf9 0x1bfead0 0x1c18bf5
0x1c18962 0x1c49bb6 0x1c48f44 0x1c48e1b 0x2317a 0x24ffc 0x26ad 0x25d5)
libc++abi.dylib: terminate called throwing an exception
Array Result: [{"ID_PROJECT":1,"NM_PROJECT":"TED"},{"ID_PROJECT":2,"NM_PROJECT":"DL"},{"ID_PROJECT":3,"NM_PROJECT":"PERD"},{"ID_PROJECT":4,"NM_PROJECT":"EPRS "},{"ID_PROJECT":5,"NM_PROJECT":"MSCS"}]
NSMutableArray *array = [Array Result JSONValue];
[array retain];
NSLog(#"Array Result: %#", array);
Related
This question already has answers here:
App Crashing: Mutating method sent to immutable object
(4 answers)
Closed 7 years ago.
I want to add an object to an existing NSMutableArray which already has data in it.
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *json = [Server getMsgRecordwithfid:self.fid];
self.msgRecords = [[NSMutableArray alloc] init];
self.msgRecords = [json objectForKey:#"msg_record"];
}
- (IBAction)sendBtn:(id)sender {
NSDictionary *json = [Server insertNewMsg:data];
[self.msgRecords addObject:json];
}
When I run the above code, the program crashes at [self.msgRecords addObject:json];. And then it gives the below error message.
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
What's wrong with my code?
What's wrong of my code?
Your code is writing over the NSMutableArray, replacing a mutable collection with an immutable one that you retrieve from json.
To fix this, call mutableCopy on the array that you retrieve:
// The first line is no longer necessary
//self.msgRecords = [[NSMutableArray alloc] init];
self.msgRecords = [[json objectForKey:#"msg_record"] mutableCopy];
I am trying to determine what is causing this error:
2014-06-08 20:40:44.076 Database[8656:70b] -[__NSArrayI length]:
unrecognized selector sent to instance 0x8a44050
Here is the code.
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
This is the breaking point where the error occurs: NSLog([json allKeys]);
SAMPLE OF THE JSON BEING USED AS INPUT:
{"1":{"key":"1","contentONE":"aaa","contentTWO":"testing"},"2":{"key":"2","contentONE":"bbb","contentTWO":null},"3":{"key":"3","contentONE":"ccc","contentTWO":"testing"}}
[json allKeys] returns an NSArray, while NSLog expects a formatting string. Try this:
NSLog(#"%#", [json allKeys]);
I need to get a count of the objects in array inside of NSDictionary. For example:
po _dictionary
{
keys = (
"one",
"two"
);
}
Taking in consideration this is an array : [_dictionary objectForKey:#"keys"]
my question is how can I get the count in the array?
I try this :
[[[_tutorials objectForKey:#"keys"] allKeys ]count];
but I'm getting this error:
* Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFArray allKeys]:
unrecognized selector sent to instance 0xa419ea0'
[_dictionary objectForKey:#"keys"] is an NSArray and not an NSDictionary. Therefore, it doesn't understand the allKeys method. Drop it and it should work:
[[_dictionary objectForKey:#"keys"] count];
Here you go:
NSLog(#"%lu", _dictionary[#"keys"].count);
NSDictionary has a method to get a NSArray of keys which I find is much more readable than using objectForKey:
[[_dictionary allKeys] count]
NSLog(#"%d", [[_dictionary objectForKey:#"keys"] count]);
As your [_dictionary objectForKey:#"keys"] is an array you can get the count directly.
NSDictionary *res=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error1];
NSLog(#"%d",[[res valueForKey:#"result"] count]);
So I am really new at developing for iOS, and I've been doing my best, to search for an answer, to debug it and anything I could come up with.
Though, I haven't been able to find a solution to the issue.
I've been trying to fetch an external JSON document, which works fine, but when it comes to parsing it, it buggers up.
First of all, this is the error message I'm getting, the whole lot of it.
2013-01-31 22:40:19.261 demodh[6205:c07] View Loaded
2013-01-31 22:40:19.479 demodh[6205:c07] -[__NSCFStringcountByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x7554f90
2013-01-31 22:40:19.480 demodh[6205:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x7554f90'
*** First throw call stack:
(0x1c93012 0x10d0e7e 0x1d1e4bd 0x1c82bbc 0x1c8294e 0x28d3 0xbd6589 0xbd4652 0xbd589a 0xbd460d 0xbd4785 0xb21a68 0x4615911 0x4614bb3 0x4652cda 0x1c358fd 0x465335c 0x46532d5 0x453d250 0x1c16f3f 0x1c1696f 0x1c39734 0x1c38f44 0x1c38e1b 0x1bed7e3 0x1bed668 0x14ffc 0x1d6d 0x1c95 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)
And this is the code I'm using at the moment:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
for(NSDictionary *dict in allDataDictionary)
{
if (![allDataDictionary isKindOfClass:[NSDictionary class]])
{
NSLog(#"2Unable to process temp array because it's an instance of %#", [allDataDictionary class]);
}
else
{
for(NSDictionary *deal in dict)
{
if (![deal isKindOfClass:[NSDictionary class]])
{
NSLog(#"Unable to process temp array because it's an instance of %#", [deal class]);
}
else
{
NSString *title = [deal objectForKey:#"title"];
NSLog(title);
}
}
}
}
}
And the JSON I'm loading is: Link
I hope you're able to assist me in finding a solution.
The problem is you're fast-enumerating a NSDictionary and expecting to get its values, when in fact you'll get its keys. When you try to fast-enumerate an NSString you get the assertion you're seeing. You probably wanted this:
for(NSObject *key in allDataDictionary) {
NSDictionary *dict = allDataDictionary[key];
...
for (NSObject *dealKey in dict) {
NSDictionary *deal = dict[dealKey];
}
...
Alternatively, if you really want to enumerate the values and don't need the keys:
for(NSDictionary *dict in [allDataDictionary allValues]) {
...
We have the following method where we are trying to access an array object at a given index. The array is resultArr. When we do a resultArr count it gives us a result of 13. So we know that the array is not null but when we try to do objectAtIndex it crashes with the error.
Function:
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray *keys = [json allKeys];
NSLog(#"keys: %#",keys);
NSArray* htmlAttributions = [json objectForKey:#"html_attributions"]; //2
NSArray* resultArr = (NSArray *)[json objectForKey:#"result"]; //2
NSArray* statusArr = [json objectForKey:#"status"]; //2
NSLog(#"htmlAttributions: %#",htmlAttributions);
NSLog(#"result: %#", resultArr); //3
NSLog(#"status: %#", statusArr); //3
NSLog(#"resultCount: %d",[resultArr count]);
[resultArr objectAtIndex:0];
}
Error:
2012-04-01 22:31:52.757 jsonParsing[5020:f803] resultCount: 13 2012-04-01 22:31:52.759 jsonParsing[5020:f803] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6d2f900 2012-04-01 22:31:52.760 jsonParsing[5020:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6d2f900'
*** First throw call stack:
Thank you.
The error message is fairly descriptive. One of the objects that your code expects to be an NSArray is actually an NSDictionary. You cannot access fields inside of an NSDictionary by using NSArray methods (and casting from NSDictionary* to NSArray* will not convert an NSDictionary into an NSArray).
This would mean that inside of the JSON, one of your elements was serialized as an object/associative array instead of as a plain array. You can easily determine which one by looking at your JSON data as text, and finding the item that uses { and } instead of [ and ].
You are saying
NSArray* resultArr = (NSArray *)[json objectForKey:#"result"]; //2
But that does not make this object ([json objectForKey:#"result"]) an NSArray. It is an NSDictionary, and sending it a message that NSDictionary does not respond to (objectAtIndex:) causes a crash.
You were able to send it the count message without crashing because NSDictionary does happen to respond to the count message. But your preconception that this is an array is still mistaken.
You cannot cast an NSDictionary* to an NSArray* as you tried to do with this line: NSArray* resultArr = (NSArray *)[json objectForKey:#"result"];, then call -objectAtIndex.