NSDictionary objectForKey/valueForKey 已格式化?

发布于 2024-10-06 00:59:40 字数 503 浏览 8 评论 0原文

我正在接收 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期以后 2024-10-13 00:59:40

两个输出只是字典中同一底层 NSString 对象的不同表示,我相信它已经从 JSON 中的任何内容重新编码为 UTF-16。

正如 Peter Hosey 对此相关问题的回答所述,NSDictionary 的输出正在对字符串进行编码,使其符合 plist 的预期文本表示形式。

在你的情况下,听起来你可能真的不想回到那个表示(尽管如果你这样做,那么看看上述问题的接受答案),而是直接在 NSString 中进行替换,像这样:

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

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

源代码中的\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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文