php - 为什么我的 json 编码对象在 Swift 中为 nil

标签 php ios json swift

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

在串行中,如果我 println(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 的常客,在发布这个问题之前尽了最大努力寻找答案。

最佳答案

您的示例数据不是有效的 JSON。看起来它可能是 JSONP,不管怎样,如果你去掉开头的 Body: Optional( 和结尾的 ) 你就会有有效的 JSON。但是,如方括号所示,JSON 是一个数组,您可以选择将其转换为字典。

您可以先尝试将其转换为 NSArray,或者转换为 [[String:String]],即 Array of String String 字典。如果你走 NSArray 路线,你可以迭代数组并将成员转换为 NSDictionaries。

关于php - 为什么我的 json 编码对象在 Swift 中为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28577064/

相关文章:

php - 如果互联网连接丢失,则在本地保存 mysql 值

ios - 'didSet' 数组中的某些元素 - Swift

ios - 仅在单元格中的特定 UIImage 上从 UITableView 推送详细 View ?

javascript - 如何使用 JSON 中的 ng-repeat 填充 <li> 中的值

c++ - 从 nlohmann json 访问元素

php - 更改图库显示缩略图的方式

php - Connection.php 第 729 行中的 QueryException : SQLSTATE[23000]: Laravel 5. 2

ios - Swift:图像缓存无法正常工作

php - MySQL 和 Python 的结构化问题

ios - 如何使用Coreplot为绘图提供多种颜色