Xcode 如何 NSLog 一个 JSON

发布于 2024-11-29 20:52:31 字数 351 浏览 5 评论 0原文

有没有办法让 NSLog 打印出整个 JSON 文件。我目前说的

NSString *deviceInfo = [NSString stringWithFormat:@"%@ %@",
                        [[UIDevice currentDevice]model], [[UIDevice currentDevice]systemVersion]];
NSDictionary *json = [deviceInfo JSONValue];
NSLog(@"json file = %@", json);

是它正在打印“json file = (null)”

谢谢 克林顿

Is there any way to get NSLog to print out an entire JSON file. I am currently saying

NSString *deviceInfo = [NSString stringWithFormat:@"%@ %@",
                        [[UIDevice currentDevice]model], [[UIDevice currentDevice]systemVersion]];
NSDictionary *json = [deviceInfo JSONValue];
NSLog(@"json file = %@", json);

And it is printing out "json file = (null)"

Thanks
Clinton

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

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

发布评论

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

评论(2

冷︶言冷语的世界 2024-12-06 20:52:31

我认为您误解了 JSON 的用途。您传递给 -JSONValue 的字符串不是有效的 JSON 字符串,因此它返回 nil。您也可以自己构建字典:

UIDevice *device = [UIDevice currentDevice];
NSDictionary *deviceInfo = [NSDictionary dictionaryWithObjectsAndKeys:[device model], @"deviceModel", [device systemVersion], @"deviceSystemVersion", nil];

然后,如果您想要对象的 JSON 字符串表示形式(例如,用于发送到服务器):

NSString *jsonDeviceInfo = [deviceInfo JSONRepresentation];

I think you’re misunderstanding what JSON is for. The string you’re passing to -JSONValue isn’t a valid JSON string, so it’s returning nil. You might as well just construct the dictionary yourself:

UIDevice *device = [UIDevice currentDevice];
NSDictionary *deviceInfo = [NSDictionary dictionaryWithObjectsAndKeys:[device model], @"deviceModel", [device systemVersion], @"deviceSystemVersion", nil];

Then if you want the JSON string representation of the object (for sending to your server, for instance):

NSString *jsonDeviceInfo = [deviceInfo JSONRepresentation];
晒暮凉 2024-12-06 20:52:31

你确定你的代码工作正常吗?你的 NSDictionary 似乎是 nil ...

你能发布 JSONValue 的实现吗?

如果对象未按预期打印,您始终可以通过扩展覆盖 -(NSString *) description 方法,它将打印您指定的方式:)

Are you sure that your code works correctly? Your NSDictionary seems to be nil ...

Could you please post the implementation of JSONValue?

If an object doesn't print as expected you can always override the -(NSString *) description method through an extension and it will print how you've specified :)

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