开发者

NSDictionary objectForKey/valueForKey already formatted?

I am receiving JSON-Data and convert it into a Dictionary. Printing the Dictionary results in the Following description:开发者_StackOverflow中文版

Objekt: {
author = blub;
authorUID = 28084;
date = "31.07.10";
numVotes = 0;
postUID = 30931;
text = "... <b>Atemtest</b> durchf\U00c3\U00bchren m\U00c3\U00bcssen?...";
timestamp = 1280585555;}

What I want is to replace the occurences of e.g. \U00c3\U00bc with \U00bc.

That's where I'm stuck. [dictionary objectForKey:@"text"] returns an already formatted String like <b>Atemtest</b> durchführen zu müssen?

valueForKey behaves the same.


Both outputs are just different representations of the same underlying NSString object in the dictionary, which I believe is already recoded as UTF-16 from whatever it was in the JSON.

As described by Peter Hosey's answer to this related question, the output from NSDictionary is encoding the string so that it conforms to the expected textual representation of a plist.

In your case, it sounds like you probably don't really want to go back to that representation (although if you do then have a look at the accepted answer to the aforementioned question), but instead do the substitution directly in the NSString, something like this:

NSString* oldStr = @"\u00c3\u00bc";
NSString* newStr = @"\u00bc";

NSString* text = [dictionary objectForKey:@"text"];
NSString* replaced = [text stringByReplacingOccurrencesOfString:oldStr withString:newStr];

The \uXXXX sequences in the source code will be converted into the relevant UTF-16 characters in the NSString constants, and these will then be matched and substituted in the target.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜