0

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
Community
  • 1
  • 1
Ale
  • 2,282
  • 5
  • 38
  • 67

2 Answers2

1

You don't have to serialize that response because it is already serialized. Afnetworking library doing all these things internally. Just use

NSArray *jsonArray = (NSArray *)responseObject;

isstead of

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: responseObject options: NSJSONReadingMutableContainers error: &e];

Edit

To access inner values, for example to access asdf. Same for others as well.

NSDictionary *testDic = (NSDictionary *)jsonArray[0];
[testDic objectForKey:@"myattr"];
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • Thanks, but how can I access my inner json objects? – Ale Aug 27 '14 at 12:06
  • Sorry but I get `-[_NSInlineData objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x15d63440` – Ale Aug 27 '14 at 12:13
  • When I try this `jsonArray[0][@"myattr"];` – Ale Aug 27 '14 at 12:16
  • @Ale Now try edited answer. I am also using same code, it is working here. May be you are using old xcode. – Yogesh Suthar Aug 27 '14 at 12:19
  • Sorry but I get the error when I try to access the jsonArray[0] value. I use xcode 5. – Ale Aug 27 '14 at 12:26
  • The debugger shows me this from the jsonArray variable `Printing description of jsonArray: <5b7b226e 616d6522 3a20224b 6174616c 6f672031 222c2022 66696c65 223a2022 68747470 3a2f2f6c 6f63616c ...` Maybe this is the problem? – Ale Aug 27 '14 at 12:30
  • @Ale Sorry for late reply. Do this thing 1) log `responseObject` and after that log `jsonArray`. 2) Check what output you get in these logs. If they are showing you json result than my code should work OR if it gives `<5b7b226e 616d6522 3a20224b 6174616c 6f672031 222c2022 66696c65 223a2022 68747470 3a2f2f6c 6f63616..` kind of response than you are getting NSData result. Kidly post your tried code in your question too. – Yogesh Suthar Aug 28 '14 at 06:54
  • thanks again, but now I get an error when I try to log the`responseObject`: `*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSInlineData getCharacters:range:]: unrecognized selector sent to instance 0x175789b0'` – Ale Aug 28 '14 at 07:52
  • @Ale For which code, kindly edit it in your question. – Yogesh Suthar Aug 28 '14 at 07:52
1

I found the solution. I don't know what is different but now the code below is working:

 [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        int statusCode = [operation.response statusCode];
        NSMutableArray *result = [[NSMutableArray alloc]init];

        NSError *e = nil;
        NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: responseObject options:NSJSONReadingMutableContainers error:&e];

        if(!jsonArray) {
           NSLog(@"Error parsing JSON: %@", e);
        }else {
            for(NSDictionary *jsonDict in jsonArray) {

                for (id key in jsonDict) {
                    id value = [jsonDict objectForKey:key];

                    if([key isEqualToString:@"name"]) {
                       //create my object...
                    }
                }
            }
        }

Thanks @Yogesh Suthar for your help.

Ale
  • 2,282
  • 5
  • 38
  • 67