swiftyjson从json数组获取字符串

时间:2017-01-24 07:24:08

标签: ios json swift xcode swifty-json

我正在使用alamofire和swiftyjson来获取它。我需要得到字符串" ubus_rpc_session",我试过这种方式,但是我得到一个数组,我需要得到字符串。你能帮帮我吗?

 Alamofire.request(URL, method: .post, parameters: param, encoding: JSONEncoding.default).responseJSON { response in
    switch response.result {
    case .success(let value):
        let json = JSON(value)
        let token = json["result"].arrayValue.map({$0["ubus_rpc_session"].stringValue})
        print(token)  
{  "jsonrpc":"2.0",
   "id":1,
   "result":[  
      0,
      {  
         "ubus_rpc_session":"07e111d317f7c701dc4dfde1b0d4862d",
         "timeout":300,
         "expires":300,
         "acls":{  
            "access-group":{  
               "superuser":[  
                  "read",
                  "write"
               ],
               "unauthenticated":[  
                  "read"
               ]
            },
            "ubus":{  
               "*":[  
                  "*"
               ],
               "session":[  
                  "access",
                  "login"
               ]
            },
            "uci":{  
               "*":[  
                  "read",
                  "write"
               ]
            }
         },
         "data":{  
            "username":"root"
         }
      }
   ]
}

1 个答案:

答案 0 :(得分:0)

试试这个

//从JSON数组中获取字符串数组(在他们的文档中)

let arrayNames =  json["users"].arrayValue.map({$0["name"].stringValue})


if let tempArray = json["result"].arrayValue {
         for item in tempArray {
              print(item)
           if let title = item["ubus_rpc_session"].string {
            println(title)
        }

         }
      }

或者检查一下

let value = tempArray[1].dictionaryObject!
                        print(value["ubus_rpc_session"]!)