Skip to content
Advertisement

Parsing JSON object with AsyncStorage

I am basically trying to write and read a simple object.

Writing: AsyncStorage.setItem('@Test', JSON.stringify(newStudent)) Reading: console.log(JSON.parse(AsyncStorage.getItem('@Test'))) But im getting “Uncaught SyntaxError: Unexpected token o in JSON at position 1”.

I Also tried console.log(AsyncStorage.getItem('@Test').ID) But im getting “Undefined”.

How can I parse the object?

Advertisement

Answer

There are two ways you can get your information from AsyncStorage

First:

JavaScript

Then you can simply call the function and catch the value in a variable

JavaScript

Second:

If you do not want to use an async function. You can create a hook that stores thaat will store the value of the variable and then update the value like so:

JavaScript

Make sure that you are importing AsyncStorage this way:

JavaScript

And not this way:

JavaScript

If you need additional information about AsyncStorage look at the docs here.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement