0
votes

I am trying to get content of "items.snippet.title" from JSON file on server.(Getting JSON Data)

but content that I will get is with Japanese and its unicode escaped so I converted text using Converting Text.

        ``Getting JSON Data``
        NSString *origin = [NSString stringWithFormat: @"URLIncluding%@",videoID];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:origin]];
        NSData *json = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSArray *array = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingAllowFragments error:nil];`enter code here`
        NSString *titleConvert=[array valueForKeyPath:@"items.snippet.title"];

        ``Converting Text``
        NSString* esc1 = [titleConvert stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
        NSString* esc2 = [esc1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
        NSString* quoted = [[@"\"" stringByAppendingString:esc2] stringByAppendingString:@"\""];
        NSData* data = [quoted dataUsingEncoding:NSUTF8StringEncoding];
        NSString* unesc = [NSPropertyListSerialization propertyListFromData:data
                                                       mutabilityOption:NSPropertyListImmutable format:NULL
                                                       errorDescription:NULL];
        assert([unesc isKindOfClass:[NSString class]]);
        NSLog(@"Output = %@", unesc);

but I've got error says

 -[__NSArrayI stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x7f99a8dc5e70

any one knows solution for this?

3
NSString *titleConvert=[array valueForKeyPath:@"items.snippet.title"]; most likely is no NSString. Set a breakpoint and find out what it is.Jörn Buitink
The error message says that the returned object is doubtless an array.valueForKeyPath – which is a KVC method – returns an array if the sender is also an array.vadian

3 Answers

0
votes

Looks like the path "items.snippet.title" returns an array. The items key is probably an array in your JSON data.

0
votes

Calling valueForKeyPath on an array returns an array. It can't be any other way because you aren't specifying how / where to index into the array. You need to deal with the array using a specific index or a loop and then navigate the key path to get your string data.

0
votes

When JSON returns an array: Iterate through the array using fast iteration, for example

for (NSDictionary* dict in array)

When you have a dictionary: Access the elements with subscription syntax, for example

NSString* aString = dict [@"title"];

Don't use methods starting with "value". You don't understand what they do. No, you don't, because if you did, you wouldn't be asking questions here. They may sometimes do what you want, which then may lead to horrible bugs later on. Apart from that they are slow :-(

The error message should be obvious: There is something that you think is an NSString, but in reality it is an NSArray. That's your fault. Sending an NSString message to the NSArray crashes.

BTW. You have an array. What makes you think it has exactly one element? Having an array would be quite pointless then. Write code that can handle multiple array elements.