Xcode how to NSLog a JSON

≯℡__Kan透↙ 提交于 2019-12-13 04:33:45

问题


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


回答1:


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];



回答2:


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 :)



来源:https://stackoverflow.com/questions/7097842/xcode-how-to-nslog-a-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!