0

I have a standard rest API setup in WP. The results are displayed in an IOS App. Now the problem occurres, that single and double quotes and & are returned in the JSON as Unicode Decimal Code: eg. &#8216. All other characters seem fine. Any Ideas to that?

2

1 Answer 1

0

I'll show here an example of how you can convert the HTML to Plain Text in Swift 4.

This example simulate that you're receiving this:

WordPress&#8216House --> WordPress'House

  1. Add this extension to convert your html code to a regular string:

`

extension Data {
    var html2AttributedString: NSAttributedString? {
        do {
            return try NSAttributedString(data: self, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            print("error:", error)
            return  nil
        }
    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }
}

extension String {
    var html2AttributedString: NSAttributedString? {
        return Data(utf8).html2AttributedString
    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }
}
  1. Use the extension

`

let htmlString = "WordPress&#8216House"

print("String is: ", textFromWP.html2String)

enter image description here

7
  • Thanks for this example. The App itself is beeing coded by another agency... They are not willing to decode in the front end. Hell...
    – Martin
    Aug 20, 2018 at 15:16
  • it is your string passed through 'wp_json_encode' or without it? Aug 20, 2018 at 15:20
  • Actually Idon't know. It is a setup of some plugins. ACF, ACF to REST API, Custom Post Type UI. It feels a bit of a Rabbit Hole to check for wp_json_encode.
    – Martin
    Aug 20, 2018 at 15:44
  • Even when I start with a clean WP install. I get the same encoding in the JSON. Is there a way to alter the JSON output? Any Idea?
    – Martin
    Aug 21, 2018 at 16:53
  • In this new WP install are you using ACF to REST API? Aug 21, 2018 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.