0

hello everyone i want to render the json response returned from server for that i'm using map method. But there is a mistake in my code please help me to figure it..The response i'm getting is

response.json

{
status: true
token:{
 access_token: "BhTErqRtliUG#8"
 client_id: "ck4fLr08YHkW2y"
 expires: "2018-02-19 03:51:50"
 scope: null
 user_id:465
},
user:{
 avatar: "https://www.indianeconomy.net/lms/wp-content/uploads/2017/11/favicon.png"
 email: "[email protected]"
 id:465
 name: "testere"
 sub:""
}
}

I've tried this

fetch(''{
.... }).then((responseData)=>{
this.setState({
        userdetail: responseData,
        loaded:true,
        })
}
render(){
return this.state.userdetail.map(user=>{
<Text>{token.access_token}</Text>
})
}

How to use map method over above json response?

2
  • You cannot loop over the object using map since you're getting an object from the response, not an array. Commented Feb 12, 2018 at 5:24
  • Also, the object is invalid in format. Commented Feb 12, 2018 at 5:25

2 Answers 2

1

The other comments are correct. You cannot map over an object like this. There are ways you can iterate over object properties:

for(let prop in object) {
  console.log(object[prop]);
}

However, in your example the objects don't have any similarities so you would not get any benefit from iterating over them.

If instead, you wanted to display the access token, you would do the following:

return (
   <Text>{this.state.userdetail.token.access_token}</Text>
);
Sign up to request clarification or add additional context in comments.

Comments

0

json string shown by you is not a valid json you can validate here https://jsonlint.com/

i am not sure about react but you mention dict so in python if you have valid json

you can map it with dict like this

json.loads(json_string)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.