NSDictionary objectForKey/valueForKey 已格式化?
我正在接收 JSON 数据并将其转换为字典。 打印字典结果如下:
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;}
我想要的是将出现的 \U00c3\U00bc
替换为 \U00bc
。
这就是我被困住的地方。 [dictionary objectForKey:@"text"]
返回一个已经格式化的字符串,如 Atemtest durchführen zu müssen?
valueForKey 的行为相同。
I am receiving JSON-Data and convert it into a Dictionary.
Printing the Dictionary results in the Following description:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两个输出只是字典中同一底层 NSString 对象的不同表示,我相信它已经从 JSON 中的任何内容重新编码为 UTF-16。
正如 Peter Hosey 对此相关问题的回答所述,
NSDictionary
的输出正在对字符串进行编码,使其符合 plist 的预期文本表示形式。在你的情况下,听起来你可能真的不想回到那个表示(尽管如果你这样做,那么看看上述问题的接受答案),而是直接在 NSString 中进行替换,像这样:
源代码中的
\uXXXX
序列将被转换为NSString
常量中的相关UTF-16字符,然后这些将在目标。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:
The
\uXXXX
sequences in the source code will be converted into the relevant UTF-16 characters in theNSString
constants, and these will then be matched and substituted in the target.