Reputation: 15
The code below only works if my JSON data is a series of integers, e.g. [11,12,13]. How could I get it to retrieve a message/phrase instead?
- (IBAction)checkmessages:(id)sender
{
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"file:///Users/Alex/Desktop/Test.json"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSError *error;
SBJSON *json = [[SBJSON new] autorelease];
NSArray *luckyNumbers = [json objectWithString:responseString error:&error];
[responseString release];
if (luckyNumbers == nil)
label.text = [NSString stringWithFormat:@"JSON parsing failed: %@", [error localizedDescription]];
else {
NSMutableString *text = [NSMutableString stringWithString:@"Latest Message:\n"];
for (int i = 0; i < [luckyNumbers count]; i++)
[text appendFormat:@"%@\n", [luckyNumbers objectAtIndex:i]];
label.text = text;
}
}
EDIT: When my JSON File looks like: [10,11,12], it works fine, but if I change it to: [Message 1,Message 2], I get the error: "JSON Parsing Failed: Expected value while parsing array"
Upvotes: 0
Views: 744
Reputation: 6981
Your JSON looks malformed,
[Message 1,Message 2]
should be
["Message 1", "Message 2"]
Upvotes: 1