Search code examples
jsonxmlreact-nativeapiaxios

Access nested data api objects in react native


I need to access the data inside the object "priceRange" inside this api data structure 'https://api.chvcfcz6km-enapsysbu1-s1-public.model-t.cc.commerce.ondemand.com/geawebservice/v2/marketplace/products/search' but I haven't found the way to do it, I'm doing an axios call in react native as I show below:

            export const RecommendationsView: React.FC = () => {
                const [fetching, setFetching] = React.useState(true)
                const navigation = useNavigation<NavigationProps>()
                const [loading, setLoading] = useState(false)
                const [items, setItems] = useState<any[]>([])

                const getData = async () => {
                    const res = await axios.get(
                        'https://api.chvcfcz6km-enapsysbu1-s1-public.model-t.cc.commerce.ondemand.com/geawebservice/v2/marketplace/products/search'
                    )
                    console.log(res.data)
                    setItems(res.data.products)
                    setLoading(true)
                    setFetching(false)
                    //este setloading me habia funcionado la idea es reemplazarlo por el setfetching
                }

                React.useEffect(() => {
                    getData()
                }, [])

                return (
                    <View>
                        <View sx={Styles.header} />
                        {items?.map((item, index) => {
                            return (
                                <View key={index}>
                                    <Text>{`${item?.name} + ${item?.priceRange}`}</Text>
                                </View>
                            )
                        })}
                    </View>
                )
            }

the call {item?.name} returns me correctly the items name, but if I do something like {item?.priceRange} or any of the data inside priceRange, it only returns [object Object] or undefined.


Solution

  • Your API return XML Response not JSON Response, you have to convert it to a json to make it work correctly.

    enter image description here