0

After get string from web service, i need to parse them. But something is going wrong.

Here is my code;

NSString *responseString = [request responseString];

    NSLog(@"response String = %@",responseString);

    NSData *tempData = [responseString dataUsingEncoding:NSUTF8StringEncoding];

    NSError *error = nil;

    NSString *innerJson = [NSJSONSerialization JSONObjectWithData:tempData
                                                          options:NSJSONReadingAllowFragments error:&error];      

    NSLog(@"innerJson = %@",innerJson);
    NSArray *entries = [NSJSONSerialization JSONObjectWithData:[innerJson dataUsingEncoding:NSUTF8StringEncoding]
                                                       options:0 error:&error];

    NSLog(@"entries = %@",entries);

    for (NSDictionary *entry in entries) {
        NSLog(@"entry = %@",entry);
        NSString *message = [entry objectForKey:@"message"];
        NSLog(@"message = %@",message );

        NSString* result = [entry objectForKey:@"result"];
        NSLog(@"result = %@", result);

    }

Here is my output;

innerJson = {"result": false,"message":"message!"}//I need parse this string.
entries = {
message = "message!";
result = 0;
}
entry = message

I am taking error in for loop. What am i doing wrong? Thanks for interest and advice.

9
  • Error is this :-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x971d640 2014-01-23 23:29:41.028 postData[10311:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x971d640' Commented Jan 23, 2014 at 21:36
  • That pretty obvious. You have an NSString, not an NSDictionary. Commented Jan 23, 2014 at 21:38
  • But 'entries' is NSArray. How can i parse it? Commented Jan 23, 2014 at 21:42
  • Entries should be NSDictionary not NSArray. Commented Jan 23, 2014 at 21:43
  • 1
    So you just used the first NSJSONSerialization pass to "unescape" the data. Commented Jan 23, 2014 at 22:08

2 Answers 2

1

The problem is that in the line

NSArray *entries = [NSJSONSerialization 

you assign the result to NSArray, and your JSON object is a dictionary, so you should assign to NSDictionary. Then the for loop is unnecessary - you can do:

NSDictionary *entries = [NSJSONSerialization JSONObjectWithData:[innerJson dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&error];

NSLog(@"entries = %@",entries);
NSString *message = [entries objectForKey:@"message"];
NSLog(@"message = %@",message );

NSString* result = [entries objectForKey:@"result"];
NSLog(@"result = %@", result);

Read more details in NSJSONSerialization documentation.

Sign up to request clarification or add additional context in comments.

8 Comments

I think it does. The loop for (NSDictionary *entry in entries) really loops through NSDictionary, so the key (which is a string) is assigned to variable of NSDictionary type, and then wrong selector is sent to it.
No, the problem is that entries contains different types of objects but the code assumes it only contains dictionaries.
The problem is that the user assumes an array when it's a dictionary, and then proceeds to loop when a loop is unnecessary to fetch those two values.
@maddy: NSJSONSerialization documentation clearly says: the top level object is an NSArray or NSDictionary. In this case it is NSDictionary. And the result of JSONObjectWithData is assigned to NSArray. The rest is outcome of this problem.
I guess in your example you missed [ ] characters. Anyway, in for JSON of such structure your original code will work, since in this case you will get NSArray consisted of two NSDictionary objects.
|
0

I'm not sure - but I guess this code should work for you:

NSString *responseString = [request responseString];
NSLog(@"response String = %@",responseString);
NSData *tempData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
/* assuming it is a dictionary */
NSDictionary *inner = [NSJSONSerialization JSONObjectWithData:tempData
                                                      options:NSJSONReadingAllowFragments error:&error];
NSLog(@"inner = %@",inner);
for(id key in inner) {
    id value = [inner objectForKey:key];
    NSLog(@"key=%@, value=%@", key, value);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.