问题
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