Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t使用SwiftyJSON访问JSON_Json_Swift_Dictionary_Swifty Json - Fatal编程技术网

Can';t使用SwiftyJSON访问JSON

Can';t使用SwiftyJSON访问JSON,json,swift,dictionary,swifty-json,Json,Swift,Dictionary,Swifty Json,我正在尝试访问通过Alamofire获得的JSON: func getDataFromServer() { Alamofire.request(.POST, websiteURL, parameters: myParameters) .responseString { (response) -> Void in if let value = response.result.value { let json = JSON(val

我正在尝试访问通过Alamofire获得的JSON:

func getDataFromServer() {
    Alamofire.request(.POST, websiteURL, parameters: myParameters) .responseString {
        (response) -> Void in
        if let value = response.result.value {
            let json = JSON(value)
            self.parseJSON(json)
        }
    }
}
,返回给我的JSON如下所示:

{  
"status":"success",
"object":[  
  {  
     "name":"Bob",
     "age":"20 ",
  },
  {  
     "name": "Jane",
     "age":"25"
  },
]
}
我正在使用SwiftyJSON访问这些名称:

func parseJSON(json: JSON) {
    for result in json["object"].arrayValue {
        print(result["name"].stringValue)
    }
}
但它没有打印任何东西。我做错什么了吗?

您的JSON无效

{
  "status": "success",
  "object": [
    {
      "name": "Bob",
      "age": "20 ",

    },
    {
      "name": "Jane",
      "age": "25"
    }, <--Delete this comma

  ]
}
{
“状态”:“成功”,
“对象”:[
{
“姓名”:“鲍勃”,
“年龄”:“20岁”,
},
{
“姓名”:“简”,
“年龄”:“25”
},您的JSON无效

{
  "status": "success",
  "object": [
    {
      "name": "Bob",
      "age": "20 ",

    },
    {
      "name": "Jane",
      "age": "25"
    }, <--Delete this comma

  ]
}
{
“状态”:“成功”,
“对象”:[
{
“姓名”:“鲍勃”,
“年龄”:“20岁”,
},
{
“姓名”:“简”,
“年龄”:“25”

},设法找出了我的代码的错误。当我使用Alamofire执行POST请求时,我将数据作为字符串返回:.responseString而不是JSON:.responseJSON

工作代码:

func getDataFromServer() {
Alamofire.request(.POST, websiteURL, parameters: myParameters) .responseJSON {
    (response) -> Void in
    if let value = response.result.value {
        let json = JSON(value)
        self.parseJSON(json)
    }
}
}

设法找出代码的错误。当我使用Alamofire执行POST请求时,我将数据作为字符串返回:.responseString而不是JSON:.responseJSON

工作代码:

func getDataFromServer() {
Alamofire.request(.POST, websiteURL, parameters: myParameters) .responseJSON {
    (response) -> Void in
    if let value = response.result.value {
        let json = JSON(value)
        self.parseJSON(json)
    }
}
}

应使用
responseJSON
代替
responseJSON

responseJSON
代替
responseJSON

responseJSON
代替
responseJSON

代替
responseString
代替
responseJSON

Opps,对不起。是手动输入的g输出JSON。但是实际的JSON中没有逗号,JSON是有效的JSON。@manohjay好的,那么alConfigure不会返回
NSData
,请参见这里的示例。我设法解决了我的问题。我是以字符串而不是JSON的形式返回数据。对不起,Opps。是手动输入JSON。但是实际的JSON中没有逗号JSON,JSON是一个有效的JSON。@manohjay好的,那么alConfigure不会返回
NSData
,请参见这里的示例我设法解决了我的问题。我是以字符串而不是JSON的形式返回数据。JSON格式不正确。这可能是问题所在吗?正确的子格式应该是{“状态”:“成功”,“对象”:[{“name”:“Bob”,“age”:“20”},{“name”:“Jane”,“age”:“25”}]}我手动键入JSON时出错,但我设法解决了我的问题。我将数据作为字符串而不是JSON返回。JSON格式不正确。这可能是问题吗?正确的son格式应该是{“status”:“success”,“object”:[{“name”:“Bob”,“age”:“20”},{“name”:“Jane”,“age”:“25”}]}我手动键入JSON时出错,但我设法解决了我的问题。我将数据作为字符串而不是JSON返回。