解析 NSStrings 以确保 XCode 中的 JSON 字符串格式正确

发布于 2024-08-20 17:43:50 字数 1340 浏览 4 评论 0原文

我正在从 PLIST 读取字符串数据,我用它来创建 JSON 字符串(顺便在 Facebook Connect 中使用)。

NSString *eventLink = [eventDictionary objectForKey:EVENT_FIND_OUT_MORE_KEY];
NSString *eventLinkEscaped = [eventLink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *eventName = [eventDictionary objectForKey:EVENT_NAME_KEY];
NSString *eventDescription = [eventDictionary objectForKey:@"Description"];
NSString *eventImageAddress = [eventDictionary valueForKey:@"Image URL"];
if ([eventImageAddress length] == 0)
{
    eventImageAddress = NO_EVENT_IMAGE_URL;
}
// Publish a story to the feed using the feed dialog
FBStreamDialog *facebookStreamDialog = [[[FBStreamDialog alloc] init] autorelease];
    facebookStreamDialog.delegate = self;
    facebookStreamDialog.userMessagePrompt = @"Publish to Facebook";
    facebookStreamDialog.attachment =[NSString stringWithFormat: @"{\"name\":\"%@\",\"href\":\"%@\",\"description\":\"%@\",\"media\":[{\"type\":\"image\",\"src\":\"%@\",\"href\":\"%@\"}]}", eventName, eventLinkEscaped, eventDescription, eventImageAddress, eventLinkEscaped];
[facebookStreamDialog show];

所有这些都运行良好,但某些事件描述(大约 150 个中的 4 个)对话框中显示的文本为空白。我找到了明显的候选者,即描述包含“字符或版权符号。我的问题是,是否有一个简单的方法调用,例如 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding 可以确保转义或忽略任何狡猾的字符?

预先致谢,

戴夫

I am reading string data from a PLIST which I am using to create a JSON string (incidentally for use within Facebook Connect).

NSString *eventLink = [eventDictionary objectForKey:EVENT_FIND_OUT_MORE_KEY];
NSString *eventLinkEscaped = [eventLink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *eventName = [eventDictionary objectForKey:EVENT_NAME_KEY];
NSString *eventDescription = [eventDictionary objectForKey:@"Description"];
NSString *eventImageAddress = [eventDictionary valueForKey:@"Image URL"];
if ([eventImageAddress length] == 0)
{
    eventImageAddress = NO_EVENT_IMAGE_URL;
}
// Publish a story to the feed using the feed dialog
FBStreamDialog *facebookStreamDialog = [[[FBStreamDialog alloc] init] autorelease];
    facebookStreamDialog.delegate = self;
    facebookStreamDialog.userMessagePrompt = @"Publish to Facebook";
    facebookStreamDialog.attachment =[NSString stringWithFormat: @"{\"name\":\"%@\",\"href\":\"%@\",\"description\":\"%@\",\"media\":[{\"type\":\"image\",\"src\":\"%@\",\"href\":\"%@\"}]}", eventName, eventLinkEscaped, eventDescription, eventImageAddress, eventLinkEscaped];
[facebookStreamDialog show];

All this works well, but certain event descriptions (4 out of approx. 150) the text that appears in the dialog is blank. I have found the obvious candidates, i.e., the description contains the " character for instance or the copyright symbol. My question is, is there an easy method call, such as stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding that will ensure that any dodgy characters are escaped or ignored?

Thanks in advance,

Dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

已下线请稍等 2024-08-27 17:43:50

我认为没有一种简单的方法可以逃避问题字符串。如果您在代码中的其他位置需要 JSON 支持,请考虑使用现有的 JSON 解析/生成器框架之一,例如 yajl-objcSBJSON。其中任何一个都可以让您将响应构建为 Foundation 对象 (NSArray/NSDictionary),然后调用单个方法来生成适当的 JSON。您的代码将更加简洁,并且您可以受益于这两个框架都经过了良好的测试。

如果只需要生成这一点 JSON,那么最好的选择可能是手动遍历输入字符串,用适当的转义版本替换潜在的问题字符。情况并不像您想象的那么糟糕。查看 SBJsonWriter 的源代码

I don't think there is an easy way to escape the problem strings. If you need JSON support anywhere else in your code, consider using one of the existing JSON parsing/generator frameworks such as yajl-objc or SBJSON. Either of these will let you build your response as Foundation objects (NSArray/NSDictionary) and then call a single method to generate the appropriate JSON. Your code will be cleaner and you have the benefit that both of these frameworks are well-tested.

If just need to generate this one bit of JSON, your best bet is probably to manually walk over the input strings, replacing potential problem characters with the appropriately escaped versions. Is is not as bad as you might think. Take a look at the source for SBJsonWriter

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