为什么我的json编码对象在Swift中为零

时间:2015-02-18 06:05:57

标签: php ios json swift

My swift code is close to the one found on this site by Jameson Quave.

如果我打印(json),则串行中的值为nil。但是如果我打印(数据),我会得到数字。如果我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的常用用户,并且在尝试发布此问题之前尽力找到答案。

1 个答案:

答案 0 :(得分:0)

您的示例数据无效JSON。看起来它可能是JSONP,无论如何,如果你摆脱了开头Body: Optional(并关闭)你就拥有了有效的JSON。但是,如方括号所示,JSON是一个数组,您可以选择将其转换为字典。

您可以先尝试将其转换为NSArray,或者将[String:String]]转换为String String字典数组。如果您使用NSArray路由,则可以迭代数组并将成员转换为NSDictionaries。