Javascript 解析json反应本机
我正在使用crypto api在我的应用程序中加载数据。在我的例子中,价格是多少 我正在尝试{item.quotes.price}但没有解决方案 我的源代码是:Javascript 解析json反应本机,javascript,json,reactjs,react-native,Javascript,Json,Reactjs,React Native,我正在使用crypto api在我的应用程序中加载数据。在我的例子中,价格是多少 我正在尝试{item.quotes.price}但没有解决方案 我的源代码是: export default class FetchExample extends React.Component { constructor(props) { super(props); this.state = { isLoading: true }; } componentDidMount() {
export default class FetchExample extends React.Component {
constructor(props) {
super(props);
this.state = { isLoading: true };
}
componentDidMount() {
return fetch("https://api.coinmarketcap.com/v2/ticker/?start=1&limit=10&sort=id&structure=array")
.then(response => response.json())
.then(responseJson => {
this.setState(
{
isLoading: false,
dataSource: responseJson.data
},
function() {
}
);
});
}
render() {
if (this.state.isLoading) {
return (
<View style={{ flex: 1, padding: 20 }}>
<ActivityIndicator />
</View>
);
}
return (
<View style={{ flex: 1, paddingTop: 20 }}>
<FlatList
data={this.state.dataSource}
renderItem={({ item }) => (
<Text>
{item.name}, {item.symbol}
</Text>
)}
keyExtractor={(item, index) => index}
/>
</View>
);
}
}
导出默认类FetchExample扩展React.Component{
建造师(道具){
超级(道具);
this.state={isLoading:true};
}
componentDidMount(){
返回取回(“https://api.coinmarketcap.com/v2/ticker/?start=1&limit=10&sort=id&structure=array")
.then(response=>response.json())
.然后(responseJson=>{
这是我的国家(
{
孤岛加载:false,
数据源:responseJson.data
},
函数(){
}
);
});
}
render(){
if(此.state.isLoading){
返回(
);
}
返回(
(
{item.name},{item.symbol}
)}
keyExtractor={(项,索引)=>index}
/>
);
}
}
有解决办法吗
谢谢你的帮助 您从请求中获得的数据在
item.quotes.UDS.price
下有price
,而不是item.quotes.price
还要确保在您的状态下初始化空的数据源数组:
class FetchExample extends React.Component {
constructor(props) {
super(props);
this.state = { isLoading: true, dataSource: [] };
}
componentDidMount() {
return fetch("https://api.coinmarketcap.com/v2/ticker/?start=1&limit=10&sort=id&structure=array")
.then(response => response.json())
.then(responseJson => {
this.setState({
isLoading: false,
dataSource: responseJson.data
});
});
}
render() {
if (this.state.isLoading) {
return (
<View style={{ flex: 1, padding: 20 }}>
<ActivityIndicator />
</View>
);
}
return (
<View style={{ flex: 1, paddingTop: 20 }}>
<FlatList
data={this.state.dataSource}
renderItem={({ item }) => (
<Text>
{item.name}, {item.symbol}, {item.quotes.USD.price}
</Text>
)}
keyExtractor={(item, index) => index}
/>
</View>
);
}
}
class-FetchExample扩展了React.Component{
建造师(道具){
超级(道具);
this.state={isLoading:true,dataSource:[]};
}
componentDidMount(){
返回取回(“https://api.coinmarketcap.com/v2/ticker/?start=1&limit=10&sort=id&structure=array")
.then(response=>response.json())
.然后(responseJson=>{
这是我的国家({
孤岛加载:false,
数据源:responseJson.data
});
});
}
render(){
if(此.state.isLoading){
返回(
);
}
返回(
(
{item.name}、{item.symbol}、{item.quotes.USD.price}
)}
keyExtractor={(项,索引)=>index}
/>
);
}
}
我看不到您使用{item.quotes.price}
的代码行。还有,你有错误吗?您的JSON看起来像什么?