Hi,
I'm building a twitter reader into an app. I'm using this JSON library to parse the twitter API. I'm seeing some odd results on certain messages. I know that the Twitter API returns results in UTF8 format. I'm wondering if I'm doing something wrong when reading the JSON parsed fields.
My code is spread out across multiple classes so it's hard to give a concise code drop with the symptoms, but here's what I've got: I am using ASIHTTP for async HTTP processing.
Here is processing a response from ASIHTTP:
...
NSMutableString* tempString = [[NSMutableString alloc]
initWithString:[request responseString]];
NSError *error;
SBJSON *json = [[SBJSON alloc] init];
id JSONresponse = [json objectWithString:tempString error:&error];
[tempString release];
[json release];
if (JSONresponse) {
self.response = JSONresponse;
...
self.response holds the JSON representation of the result from the Twitter call.
Now, I will take the JSON response and write each tweet into a container object (Tweet).
in the following code, the response from above is referenced as request.response:
...
// save list of albums to local cache
for (NSDictionary* response in request.response) {
Tweet* tweet = [[Tweet alloc] init];
tweet.text = [response objectForKey:@"text"];
tweet.id = [response objectForKey:@"id"];
tweet.created = [response objectForKey:@"created_at"];
[Tweet addTweet:tweet];
[tweet release];
}
...
at this point, I have a container holding the tweets. I'm only keeping 3 fields from the tweet: "id", "text", and "created_at". the "text" field is the problem field.
To display the tweets, I build an HTML page from the container of tweets, like this:
...
Tweet* tweet = nil;
for (int i = 0; i < [Tweet tweetCount]; i++) {
tweet = [Tweet tweetAtIndex:i];
[html appendString:@"<div class='tweet'>"];
[html appendFormat:@"<div class='tweet-date'>%@</div>", tweet.created ];
[html appendFormat:@"<div class='tweet-text'>%@</div>", tweet.text ];
[html appendString:@"</div>"];
}
...
In another routine, I save the HTML page to a temp file.
if (html && [html length] > 0 ) {
NSString* uniqueString = [[NSProcessInfo processInfo] globallyUniqueString];
NSString* filename = [NSString stringWithFormat:@"%@.html", uniqueString ];
filename = [tempDir stringByAppendingPathComponent:filename];
NSError* error = nil;
[html writeToFile:filename
atomically:NO
encoding:NSUTF8StringEncoding
error:&error];
...
I then create a URLRequest from the file and load it into an UIWebview:
NSURL* url = [NSURL fileURLWithPath:filename];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
...
At this point, I can see the tweets in a browser window. some of the tweets will show invalid characters like this:
iPhone 4 ad spoofed with Glee’s Jane Lynch ...
Glee’s should be Glee's
Can anybody shed any light on what I'm doing wrong and offer suggestions on how to fix?
basically, to summarize:
I'm reading a UTF8 feed with JSON
I write the UTF8 strings into an HTML file
I display the HTML file with UIWebview.
some of the UTF8 strings are not properly decoded. I need to know
where to decode them and how to do it.
thanks!
Mark