5

I'm currently making a request to a URL.

One of the teams has the latin character Ñ which appears to be making my JSON nil and as a result no data is displayed in the table to which I am exporting the data. I've done some research and I believe that I need to encode it as NSISOLatin1StringEncoding.

I am using SwiftyJSON to parse the JSON.

let cuartoURL = NSURL(string: cuartoURLString)

    //initializes request
    let request = NSURLRequest(URL: cuartoURL!)
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.currentQueue()) { response, jsonDataRequest4, error in
        if jsonDataRequest4 != nil {

        let dataRequest4 = jsonDataRequest4
        //println(NSString(data:dataRequest4, encoding: NSUTF8StringEncoding))

        //takes data, saves it as json
        let cuartoJSON = JSON(data: jsonDataRequest4)

        //checks to see that contents != nil, meaning the JSON file was found
        if cuartoJSON != nil {
            equiposList.removeAll(keepCapacity: false)
            //counts number of teams
            numeroDeEquipos = cuartoJSON["lista-equipos"].count
            println(numeroDeEquipos)

            //saves each variable and appends to a array
            for var index = 0; index < numeroDeEquipos;++index {
                var equipoID = Int(cuartoJSON["lista-equipos"][index]["EquipoID"].number!)
                var nomEquipo = cuartoJSON["lista-equipos"][index]["nomEquipo"].string
                var nomGrupo = cuartoJSON["lista-equipos"][index]["nomGrupo"].string

                var equiposNuevo = listaEquipos(equipoID: equipoID, nomEquipo: nomEquipo!, nomGrupo: nomGrupo!)
                equiposList.append(equiposNuevo)
                self.tableView.reloadData()
            }
            //loadingActivity.hideLoadingActivity(success: true, animated: false)
            //reloads data once json is complete
            self.tableView.reloadData()
        } else {
            //loadingActivity.hideLoadingActivity(success: false, animated: true)
            println("NIL JSON")
            }
        }
FredLoh
  • 1,804
  • 18
  • 27

2 Answers2

3

JSON is a binary format and has no concept of text encoding (as can be deduced by its mime type starting with application/ rather than text/. JSON is always encoded as Unicode (UTF-8, UTF-16 or UTF-32) as is very clear from the specification (section 8.1).

It may be that the server sends you invalid JSON (incorrectly coded as Latin-1 which will probably make it look like bad UTF-8 to the parser). The remedy would then be

  1. Fix the server.
  2. If failing 1., you need some kind of hack:
    1. Convert NSData to NSString using Latin1 character encoding
    2. Convert NSString to NSData using UTF-8 character encoding
    3. Parse JSON
Community
  • 1
  • 1
Krumelur
  • 31,081
  • 7
  • 77
  • 119
  • Due to the nature of the names/etc of people that we are dealing with we use ISO-8859-1 as the encoding. I'm using swiftyJSON to parse the JSON, is there any way for me to simply change the encoding that swiftyJSON uses? Here is the source file: https://raw.githubusercontent.com/SwiftyJSON/SwiftyJSON/master/Source/SwiftyJSON.swift – FredLoh Jul 22 '15 at 00:51
  • Noo need, UTF-8 supports all characters that Latin-1 can represent and much more. – Krumelur Jul 24 '15 at 05:51
1

This worked for me:

xhr.overrideMimeType("application/json;charset=iso-8859-1");

from: Fetch json with non ASCI characters like ü, chrome displays network displays correctly

Community
  • 1
  • 1
partizanos
  • 1,064
  • 12
  • 22