我遇到了以下问题....我 NSDictionary
是这样的:
var dic : NSDictionary = [ "level" : [ ["column" : 0,"down" : 0,"left" : 0,"right" : 0,"row" : 0,"up" : 0], ["column" : 1,"down" : 0,"left" : 0,"right" : 0,"row" : 0,"up" : 0], ["column" : 2,"down" : 0,"left" : 0,"right" : 0,"row" : 0,"up" : 0], ["column" : 0,"down" : 0,"left" : 0,"right" : 0,"row" : 1,"up" : 0], ["column" : 1,"down" : 0,"left" : 0,"right" : 0,"row" : 1,"up" : 0], ["column" : 2,"down" : 0,"left" : 0,"right" : 0,"row" : 1,"up" : 0] ] ]
但如果我打印这个,
print(dic); or print(“\(dic)”);
Out put是这样的:
{ level = ( { column = 0; down = 0; left = 0; right = 0; row = 0; up = 0; }, { column = 1; down = 0; left = 0; right = 0; row = 0; up = 0; }, { column = 2; down = 0; left = 0; right = 0; row = 0; up = 0; }, { column = 0; down = 0; left = 0; right = 0; row = 1; up = 0; }, { column = 1; down = 0; left = 0; right = 0; row = 1; up = 0; }, { column = 2; down = 0; left = 0; right = 0; row = 1; up = 0; } ); }
我怎样才能获得精确的Json字符串?在swift中,xcode?
无需实现这种复杂的逻辑,
你可以这么做
var jsonData: NSData = NSJSONSerialization.dataWithJSONObject(dictionary, options: NSJSONWritingOptions.PrettyPrinted, error: &error)! if error == nil { return NSString(data: jsonData, encoding: NSUTF8StringEncoding)! as String }
如果你想用API将它发送到服务器,甚至不需要将它转换为String