我正在尝试制作一个应用程序,使用 wiki quote api 显示名人(例如 Bob Dylan)的名言。
请求 url 如下所示:
https://en.wikiquote.org/w/api.php?format=json&action=parse&page=Bob_Dylan&prop=text
我有 React Native 请求:
componentWillMount() {
Api().then((response) => {
this.setState({
quote:response.parse.text
})
});
}
但是,当我尝试显示引号时,它以对象的形式出现并在 View 中获取 [object object]。在我收到的控制台中:
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 ofText
.
当我尝试 response.parse.text.*
时,我收到一个 token 错误。有什么建议可以解决这个问题吗?
最佳答案
您必须通过调用其 json()
方法显式地将 response
转换为 Object
,例如:
var url = 'https://en.wikiquote.org/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))
关于javascript - 如何在 React Native 中访问 json 对象的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38132330/