简体   繁体   English

swiftyjson从json数组获取字符串

[英]swiftyjson getting string from json array

I am using alamofire and swiftyjson to get it. 我正在使用alamofire和swiftyjson来获取它。 I need to get string "ubus_rpc_session", I tryed this way, but I get an array, I need to get string. 我需要得到字符串“ubus_rpc_session”,我尝试这种方式,但我得到一个数组,我需要得到字符串。 Could you help me? 你可以帮帮我吗?

 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"
         }
      }
   ]
}

Try this 尝试这个

//Getting an array of string from a JSON Array(In their documents) //从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)
        }

         }
      }

Or check this 或者检查一下

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM