use*_*388 3 parsing objective-c nsstring ios sbjson
我正在开发应用程序,我在其中存储NSDictionary中的文件名和文件路径.我的字典就像,
Dict Path : {
background = "file://localhost/var/mobile/Applications/6118A03F-345B-42D5-AC19-25F6D9AC4484/Documents/background.caf";
bgMusic = "file://localhost/var/mobile/Applications/6118A03F-345B-42D5-AC19-25F6D9AC4484/Documents/bgMusic.caf";
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但当我尝试在JSON字符串中转换字典时,
NSString *strPathForSong = [json stringWithObject:dictPath];
NSLog(@"path sting : %@",strPathForSong);
Run Code Online (Sandbox Code Playgroud)
它返回null字符串.那么有没有办法将带有"/"字符串的字典转换为json字符串?先感谢您
Tho*_*ing 10
将字典转换为JSON字符串时,路径分隔符不应该是一个问题.
您的示例未显示json变量的类型和初始化,但您可以通过以下方式从dict获取JSON表示:
NSDictionary* jsonDict = @{ @"background": @"file://localhost/var/mobile/Applications/6118A03F-345B-42D5-AC19-25F6D9AC4484/Documents/background.caf",
@"bgMusic": @"file://localhost/var/mobile/Applications/6118A03F-345B-42D5-AC19-25F6D9AC4484/Documents/bgMusic.caf"};
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:nil];
NSString* jsonString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
NSLog(@"Dict:%@", jsonString);
Run Code Online (Sandbox Code Playgroud)
这在这里工作正常(包括适当的转义日志行中的路径分隔符)