javascript - How do I access string of json object in React Native - Stack Overflow

I am trying to make an app that displays quotes from a famous person (e.g.Bob Dylan) using the wiki q

I am trying to make an app that displays quotes from a famous person (e.g. Bob Dylan) using the wiki quote api.

The request url looks like this:

.php?format=json&action=parse&page=Bob_Dylan&prop=text

I have the React Native request working with:

ponentWillMount() {
Api().then((response) => {
  this.setState({
    quote:response.parse.text

  })

});
}

When I try to display the quote, however, it is in the form of an object and get [object object] in the view. In the console I receive:

Possible Unhandled Promise Rejection (id: 0): Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of Text. Invariant Violation: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of Text.

When I try response.parse.text.* I receive a token error. Any suggestions how to fix this ?

I am trying to make an app that displays quotes from a famous person (e.g. Bob Dylan) using the wiki quote api.

The request url looks like this:

https://en.wikiquote/w/api.php?format=json&action=parse&page=Bob_Dylan&prop=text

I have the React Native request working with:

ponentWillMount() {
Api().then((response) => {
  this.setState({
    quote:response.parse.text

  })

});
}

When I try to display the quote, however, it is in the form of an object and get [object object] in the view. In the console I receive:

Possible Unhandled Promise Rejection (id: 0): Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of Text. Invariant Violation: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of Text.

When I try response.parse.text.* I receive a token error. Any suggestions how to fix this ?

Share Improve this question edited Jun 30, 2016 at 20:07 Nathaniel Ford 21.3k20 gold badges96 silver badges110 bronze badges asked Jun 30, 2016 at 20:05 crodcrod 2453 gold badges5 silver badges12 bronze badges 1
  • 1 response.parse.text['*'] .. * is a special character – azium Commented Jun 30, 2016 at 20:14
Add a ment  | 

2 Answers 2

Reset to default 3

You have to explicitly turn response into Object , by calling its json() method, like:

 var url = 'https://en.wikiquote/w/api.php?format=json&action=parse&page=Bob_Dylan&prop=text';

 fetch(url)
   .then((response) => response.json())
   .catch((error) => console.warn("fetch error:", error))
   .then((response) => console.log(response.parse))

Working example: https://rnplay/apps/EqyMdA

You can use response.text() instead of response.json()

var url = 'https://en.wikiquote/w/api.php?
 fetch(url)
   .then((response) => response.text())
   .catch((error) => console.warn("fetch error:", error))
   .then((response) => console.log(response.parse))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744338480a4569257.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信