I'm using AFNetworking 2.0
to call an REST API with returns an valid JSONArray
like:
[{"myattr": "asdf"}, {"myattr": "jklo"}]
I parse my responseObject
with this loc:
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: responseObject options: NSJSONReadingMutableContainers error: &e];
The weird thing is that my jSONArray
(looked up in the debugger) contains two objects but the serializer throws a parsing error. What happens is that some of the inner objects attributes are stripped of their ticks. So for example I see:
[{attr: "asdf", attr2: "jklo", "attr3": "tzze"}, ...]
The odd thing is here, that some of the attributes keep their ticks!
After all the only idea I have is (except that this whole thing is buggy) that the serializer doesn't anticipate the JSONArray
but an JSONObject
.
Here's my whole code:
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
int statusCode = [operation.response statusCode];
NSError *e = nil;
// NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: responseObject options: NSJSONReadingMutableContainers error: &e];
NSArray *jsonArray = (NSArray *)responseObject;
NSLog(responseObject);
NSLog(jsonArray);
NSDictionary *testDic = (NSDictionary *)jsonArray[0];
[testDic objectForKey:@"name"];
if (!jsonArray) {
NSLog(@"Error parsing JSON: %@", e);
} else {
for(NSDictionary *item in jsonArray) {
NSLog(@"Item: %@", item);
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
So, after all I'm just looking for a solution to parse my JSONArray
. I looked here, here and here but didn't find a solution...
Thanks!
Edit
Here is the parsing error:
(<invalid>) <error: expected ']' error: 1 errors parsing expression