How to decode the questions and answers returned from the api call?


#1

I’m creating a react native mobile app and I’m using the open trivia db API. I seem to be stuck on decoding the questions and answers. Right now its displaying

Which Shakespeare play inspired the musical 'West Side Story'?

I would like to display the decoded version

Which Shakespeare play inspired the musical 'West Side Story'?

This is the component I have rendering to the screen

import React from 'react'
import { View, Text } from 'react-native'

export default class Question extends React.Component<*> {
    render() {
        return (
            <View>
                <View>
                    <Text>Category: {this.props.category}</Text>
                    <Text>Question #: {this.props.nr} of {this.props.total}</Text>
                </View>
                <View>
                    <Text>{decodeURI(this.props.question)}</Text>
                </View>
            </View>
        )
    }
}

If I were to replace decodeURI() with encodeURI() it encodes it but for some reason seems to ignore the decode function.

Any help would be greatly appreciated.

Thanks


#2

Try putting unescape() around the decodeURI.


#3

Thanks @The_Sink_God. I tried unescape() but that wasn’t working and the docs for unescape() say that javascript depreciated that function in javascript v1.5.

I figured it out though. If anyone else has the problem here’s the solution that worked for me.

I actually had to encode the actual API call itself with url3986 and then wrap decodeURIComponent() around what I wanted printed to the screen.

Cheers


#4

Oh wow, I never knew it was deprecated. Glad you got it working though.