繁体   English   中英

如何在swift3.0中使用nonjson数据

[英]How to work with nonjson data in swift3.0

我面临来自swft3.0中支持的非json数据的一些问题。

这是我正在使用http://api.geonames.org/export/geonamesData.js?username=orakzai的Api。

响应是:

var geonamesPostalCodeCountries = ["AD","AR","AS","AT","AU","AX","BD","BE","BG","BR","BY","CA","CH","CO","CR","CZ","DE","DK","DO","DZ","ES","FI","FO","FR","GB","GF","GG","GL","GP","GT","GU","HR","HU","IE","IN","IS","IT","JE","JP","LI","LK","LT","LU","MC","MD","MK","MP","MQ","MT","MX","MY","NC","NL","NO","NZ","PH","PK","PL","PM","PR","PT","RE","RO","RU","SE","SI","SK","SM","TH","TR","US","VA","VI","WF","YT","ZA","GR"]; var geonamesUserIpCountryCode='null';

请找到下面的代码。

callApi(apiName: "http://api.geonames.org/export/geonamesData.js?username=orakzai")    

 func callApi(apiName :String) {
   let reachability = Reachability()
   if !(reachability?.isReachable)! {
       showAlertView(message: " We are unable to reach servers. Please check your network/internet connection.")
   } else {
       let url = URL(string: "\(apiName)")

       let session = URLSession.shared // or let session = URLSession(configuration: URLSessionConfiguration.default)

       if let usableUrl = url {
           let task = session.dataTask(with: usableUrl, completionHandler: { (data, response, error) in
               print(response)
               if let data = data {
                   if let stringData = String(data: data, encoding: String.Encoding.utf8) {
                       self.dataArray.append(stringData)
                       print(self.dataArray)
                   }
               }
           })
           task.resume()
       }
   }

当我使用此代码时,我得到以下响应

["var geonamesPostalCodeCountries = [\\"AD\\",\\"AR\\",\\"AS\\",\\"AT\\",\\"AU\\",\\"AX\\",\\"BD\\",\\"BE\\",\\"BG\\",\\"BR\\",\\"BY\\",\\"CA\\",\\"CH\\",\\"CO\\",\\"CR\\",\\"CZ\\",\\"DE\\",\\"DK\\",\\"DO\\",\\"DZ\\",\\"ES\\",\\"FI\\",\\"FO\\",\\"FR\\",\\"GB\\",\\"GF\\",\\"GG\\",\\"GL\\",\\"GP\\",\\"GT\\",\\"GU\\",\\"HR\\",\\"HU\\",\\"IE\\",\\"IN\\",\\"IS\\",\\"IT\\",\\"JE\\",\\"JP\\",\\"LI\\",\\"LK\\",\\"LT\\",\\"LU\\",\\"MC\\",\\"MD\\",\\"MK\\",\\"MP\\",\\"MQ\\",\\"MT\\",\\"MX\\",\\"MY\\",\\"NC\\",\\"NL\\",\\"NO\\",\\"NZ\\",\\"PH\\",\\"PK\\",\\"PL\\",\\"PM\\",\\"PR\\",\\"PT\\",\\"RE\\",\\"RO\\",\\"RU\\",\\"SE\\",\\"SI\\",\\"SK\\",\\"SM\\",\\"TH\\",\\"TR\\",\\"US\\",\\"VA\\",\\"VI\\",\\"WF\\",\\"YT\\",\\"ZA\\",\\"GR\\"];\\nvar geonamesUserIpCountryCode=\\'null\\';\\n"]

我想将var geonamesPostalCodeCountrie更改为tableview

假设您具有以下字符串变量:

let response = "var geonamesPostalCodeCountries = [\"AD\",\"AR\",\"AS\",\"AT\",\"AU\",\"AX\",\"BD\",\"BE\",\"BG\",\"BR\",\"BY\",\"CA\",\"CH\",\"CO\",\"CR\",\"CZ\",\"DE\",\"DK\",\"DO\",\"DZ\",\"ES\",\"FI\",\"FO\",\"FR\",\"GB\",\"GF\",\"GG\",\"GL\",\"GP\",\"GT\",\"GU\",\"HR\",\"HU\",\"IE\",\"IN\",\"IS\",\"IT\",\"JE\",\"JP\",\"LI\",\"LK\",\"LT\",\"LU\",\"MC\",\"MD\",\"MK\",\"MP\",\"MQ\",\"MT\",\"MX\",\"MY\",\"NC\",\"NL\",\"NO\",\"NZ\",\"PH\",\"PK\",\"PL\",\"PM\",\"PR\",\"PT\",\"RE\",\"RO\",\"RU\",\"SE\",\"SI\",\"SK\",\"SM\",\"TH\",\"TR\",\"US\",\"VA\",\"VI\",\"WF\",\"YT\",\"ZA\",\"GR\"];\nvar geonamesUserIpCountryCode=\'null\';\n"

由于在Swift中,您不能像JavaScript中那样简单地使用eval(response);来评估响应字符串eval(response); (可以创建与字符串中声明的变量匹配的动态变量),如何处理:

let vars = response.components(separatedBy: "\n")

var geonamesPostalCodeCountriesStr = vars[0].components(separatedBy: "=")[1].trimmingCharacters(in: CharacterSet(charactersIn: " [];")).replacingOccurrences(of: "\"", with: "")
var geonamesPostalCodeCountries: [String] = geonamesPostalCodeCountriesStr.components(separatedBy: ",")

var geonamesUserIpCountryCodeStr = vars[1].components(separatedBy: "=")[1].trimmingCharacters(in: CharacterSet(charactersIn: " [];")).replacingOccurrences(of: "\'", with: "")
var geonamesUserIpCountryCode: String? = (geonamesUserIpCountryCodeStr == "null") ? nil : geonamesUserIpCountryCodeStr

暂无
暂无

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

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