【发布时间】:2015-02-18 06:05:56
【问题描述】:
My swift code is close to the one found on this site by Jameson Quave.
如果我 println(json) 串行,则值为 nil。但是,如果我 println(data),我会得到数字。如果我println("Body: \(strData)") 我得到以下信息。任何帮助将不胜感激。
这里是示例数据:
Body: Optional([
{
"LocAddress": "2045 Broadway ",
"City": "New York",
"Region": "New York",
"Country": "United States"
},
{
"LocAddress": "6916 4th Ave",
"City": "New York",
"Region": "New York",
"Country": "United States"
},
{
"LocAddress": "Bleaker St.",
"City": "New York",
"Region": "New York",
"Country": "United States"
},
{
"LocAddress": "37-45 87th St",
"City": "New York",
"Region": "New York",
"Country": "United States"
},
{
"LocAddress": "88-35 249th St",
"City": "New York",
"Region": "New York",
"Country": "United States"
},
{
"LocAddress": "225 West 79th",
"City": "New York",
"Region": "New York",
"Country": "United States"
}
])
如果有人问过这个问题,我深表歉意。我是 stackoverflow 的常客,在发布这个问题之前,我已尽力寻找答案。
【问题讨论】:
-
如果你看我发的链接,看看哪里有语句 println("Body: (strData)")。 'Body:' 部分来自那里, Optional() 来自 swift 编译器。我可以简单地添加一个!在行尾标记 var strData = NSString(data: data, encoding: NSUTF8StringEncoding)!它会照顾可选的。不管这两件事,我的 strData 是 JSON 格式,那么为什么当我做 var json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as 时呢? NSDictionary 然后 println(json),是 nil 吗?
-
你拥有的是 JSON array 而不是 dictionary。