Swift、JSON 解析不适用于 ÆØÅ
Swift, JSON parsing not working with ÆØÅ
我正在努力学习 Swift 和 IOS 开发。现在我已经为一个简单的测验应用苦苦挣扎了几天。我已经让应用程序正常运行,我什至设法将它连接到数据库。现在的问题是,使用 JSON 解析我可以从数据库中很好地解析文本,直到我用普通文本和字符替换我的虚拟文本。解析脚本如下所示:
let urlPath = "xxxx/json.php"
let url: NSURL = NSURL(string: urlPath)!
let session = NSURLSession.sharedSession()
var questionNummer = 0
// let sporsmalArray=[question]()
typealias questionArray = [AnyObject] // or whatever is inside, maybe String
println("starting web request")
let task = session.dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in
println("passed web request")
if (error != nil) {
// If there is an error in the web request, print it to the console
println("WebError: " + error.localizedDescription)
}else {
println("no web request error")
var err: NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
if err != nil {
// If there is an error parsing JSON, print it to the console
println("JSON Error \(err!.localizedDescription)")
}
else {
println("no json error")
let questions=jsonResult["data"] as? [[String:String]]
if(questions == nil){println("questions is nil")}
if (questions != nil) {
for question in questions! {
println("for: question")
questionNummer += 1
let answer1=question["answerOne"]!
let answer2=question["answerTwo"]!
let answer3=question["answerThree"]!
let answer4=question["answerFour"]!
let aidd=question["id"]!
let questionItself=question["questionTemplate"] as String!
let correctAnswerJson=question["correctAnswer"]
println("question parsed")
var newQuestion = [questionItself , answer1 , answer2 , answer3 , answer4, correctAnswerJson]
var questionArray = Array<myspecialarray>();
questionArray.append(newQuestion)
if(questionArray.isEmpty) {println("questionArray is null")}
else{println("questionArray is not null")}
self.sporsmalArray.append(questionArray)
println("Neste printer sporsmal arrayen:")
println(self.sporsmalArray)
println("Arrayen er ferdig")
//Jeg kaller på intial state her for å få med verdiene i arrayen
self.initialState()
}
}else{
println("Kjører ikke")}
}
}
})
task.resume()
如果数据库包含 æøå 脚本将不会向我的数组添加任何内容。我的打印行显示脚本有效,但是当我打印数组时它是空的。
starting web request
Dette teller påå spørsmålsarrayen: 0
passed web request
no web request error
no json error
questions is nil
当我从数据库中删除北欧字母时,脚本再次正常运行。
starting web request
Dette teller påå spørsmålsarrayen: 0
passed web request
no web request error
no json error
for: question
question parsed
questionArray is not null
Neste printer sporsmal arrayen:
[(
(
"Dettekommerfrainternet ! ",
as1nett,
aa2nett,
aa3nett,
aa4nett,
4
)
)]
数据库字符集设置为 varchar 和瑞典语 utf-8。
这个问题是什么?
事实证明这是我的 php 到 Json 脚本的错误。
通过放置 mysqli_set_charset($con, "utf8");
连接解决后。
我正在努力学习 Swift 和 IOS 开发。现在我已经为一个简单的测验应用苦苦挣扎了几天。我已经让应用程序正常运行,我什至设法将它连接到数据库。现在的问题是,使用 JSON 解析我可以从数据库中很好地解析文本,直到我用普通文本和字符替换我的虚拟文本。解析脚本如下所示:
let urlPath = "xxxx/json.php"
let url: NSURL = NSURL(string: urlPath)!
let session = NSURLSession.sharedSession()
var questionNummer = 0
// let sporsmalArray=[question]()
typealias questionArray = [AnyObject] // or whatever is inside, maybe String
println("starting web request")
let task = session.dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in
println("passed web request")
if (error != nil) {
// If there is an error in the web request, print it to the console
println("WebError: " + error.localizedDescription)
}else {
println("no web request error")
var err: NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
if err != nil {
// If there is an error parsing JSON, print it to the console
println("JSON Error \(err!.localizedDescription)")
}
else {
println("no json error")
let questions=jsonResult["data"] as? [[String:String]]
if(questions == nil){println("questions is nil")}
if (questions != nil) {
for question in questions! {
println("for: question")
questionNummer += 1
let answer1=question["answerOne"]!
let answer2=question["answerTwo"]!
let answer3=question["answerThree"]!
let answer4=question["answerFour"]!
let aidd=question["id"]!
let questionItself=question["questionTemplate"] as String!
let correctAnswerJson=question["correctAnswer"]
println("question parsed")
var newQuestion = [questionItself , answer1 , answer2 , answer3 , answer4, correctAnswerJson]
var questionArray = Array<myspecialarray>();
questionArray.append(newQuestion)
if(questionArray.isEmpty) {println("questionArray is null")}
else{println("questionArray is not null")}
self.sporsmalArray.append(questionArray)
println("Neste printer sporsmal arrayen:")
println(self.sporsmalArray)
println("Arrayen er ferdig")
//Jeg kaller på intial state her for å få med verdiene i arrayen
self.initialState()
}
}else{
println("Kjører ikke")}
}
}
})
task.resume()
如果数据库包含 æøå 脚本将不会向我的数组添加任何内容。我的打印行显示脚本有效,但是当我打印数组时它是空的。
starting web request
Dette teller påå spørsmålsarrayen: 0
passed web request
no web request error
no json error
questions is nil
当我从数据库中删除北欧字母时,脚本再次正常运行。
starting web request
Dette teller påå spørsmålsarrayen: 0
passed web request
no web request error
no json error
for: question
question parsed
questionArray is not null
Neste printer sporsmal arrayen:
[(
(
"Dettekommerfrainternet ! ",
as1nett,
aa2nett,
aa3nett,
aa4nett,
4
)
)]
数据库字符集设置为 varchar 和瑞典语 utf-8。
这个问题是什么?
事实证明这是我的 php 到 Json 脚本的错误。 通过放置 mysqli_set_charset($con, "utf8"); 连接解决后。