我遇到以下问题....
我的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 out就是这样:
{
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? p>
答案 0 :(得分:28)
无需实现这种复杂的逻辑,
你可以简单地这样做
var jsonData: NSData = NSJSONSerialization.dataWithJSONObject(dictionary, options: NSJSONWritingOptions.PrettyPrinted, error: &error)!
if error == nil {
return NSString(data: jsonData, encoding: NSUTF8StringEncoding)! as String
}
如果您想将API与服务器一起发送,则无需将其转换为String