Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6212

have to JSON.parse twice to extract json object from stringified QR Code

$
0
0

I have some JSON

{    id: "abcd",    type: "receive"}

which I stringified and generated a QR code using some online QR code maker. In my react native app, I am using react-native-qrcode-scanner to scan this qr code which calls an onSuccess(qrCode: Event) function.

I've set up the function as follows:

function onSuccess(qrCode: Event) {    console.log(qrCode.data);    const parsedData = JSON.parse(qrCode.data);    console.log(parsedData);    console.log(typeof parsedData);    console.log(parsedData.id);    const parsedData2 = JSON.parse(parsedData);    console.log(parsedData2.id);    navigation.navigate('Home');  }

This gives the following output

"{\"type\":\"receive\",\"id\":\"abcd\"}"{"type":"receive","id":"abcd"}stringundefinedabcd

So basically, qrCode.data is a string which is expected. parsedData outputs like an object, but has typeofstring. Accessing properties on parsedData gives undefined - which makes sense if it's a string. When I parse it a second time into parsedData2 that is an object and I can access properties on it.

My confusion is, why is it not just parsing it the first time? Why do I have to do it twice?


Viewing all articles
Browse latest Browse all 6212

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>