Swift,JSON解析不适用于ÆØÅ

时间:2015-01-21 12:34:37

标签: json database parsing swift

我正在尝试学习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和swedish utf-8。

这个问题是什么?

1 个答案:

答案 0 :(得分:0)

这对我的php到Json脚本是一个错误。 通过放置mysqli_set_charset($ con,&#34; utf8&#34;); 连接解决后。