Skip to content
Advertisement

how to work with json elements in react native

Sending data into the API with the following code

    signIn: async (email, senha) =>{
    try{
        const req = await fetch ( `${BASE_API}/Auth/login.php`,{
             method: 'POST',
             headers:{ Accept: 'application/json', 'Content-Type': 'application/json' },
             body: JSON.stringify({email: email, senha: senha})
         });

         const json = await req.json();
         console.log(json);
         return json;
     } 
     catch(error){
         console.error(error);
     }
}

returns the following json that is being trown into console.

Array [
  Object {
    "user_avatar": "path",
    "user_celular": "00912341234",
    "user_email": "test@gmail.com",
    "user_id": "6",
    "user_mesa": "0",
    "user_nome": "Marcos",
    "user_senha": "testtest",
    "user_since": "2021-04-28",
    "user_sobrenome": "da Silva",
    "user_tipo": "0",
  },
]

how can I extract data from this json? for example: alert(json.user_nome)

Advertisement

Answer

First thing is this is not json this is the array object so if you want data from 1st object in array then you should use this…

json[0].user_nome 

Or if you want all the user_nome property from all json object data then you have to use loop.

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