【问题标题】:How would I access this JSON file using React Native我将如何使用 React Native 访问这个 JSON 文件
【发布时间】:2018-12-10 22:15:11
【问题描述】:

我有一个使用标签栏的正常反应本机应用程序。在我当前的版本中,我可以让它显示我从 youtube 视频中找到的 VSON 的平面列表。我现在想使用自己的 JSON 列表,但遇到了问题。

这里是在线 JSON 文件的链接:api.jsonbin.io/b/5b3a49c8a5a2f34ea6b0fbc8

这是我从 youtube 视频中获得的正在处理的特定文件的代码(这有效并显示名称列表)。请注意,这是我的标签栏中的一个标签:

我的问题是我需要设置什么 this.setState({ 数据: json.results }) 到,然后我要设置什么 {${item.name.first} ${item.name.last}} }

如果我只是想显示药物可编码概念

import React, { Component } from "react";
import {
View,
Text,
StyleSheet,
FlatList
} from "react-native";

import { Icon } from 'native-base'


class LikesTab extends Component {

//SETTING THE STATE MAKING AN EMPTY ARRAY WHICH WE FIL
  state = {
    data: []
  };

  componentWillMount() {
this.fetchData();
  }

//Getting the data
  fetchData = async () => {
const response = await fetch("https://randomuser.me/api?results=500");
const json = await response.json();
this.setState({ data: json.results });
  };

  //var customData = require('./customData.json');


//Setting what is shown
  render() {
return (
  <View style={styles.container}>
    <FlatList
      data={this.state.data}
      keyExtractor={(x, i) => i}
      renderItem={({ item }) =>
        <Text>
          {`${item.name.first} ${item.name.last}`}
        </Text>}
    />
  </View>
);
  }
}
export default LikesTab;

const styles = StyleSheet.create({
container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center'
}
});

这是我当前的代码,它只显示一个空白屏幕:

import React, { Component } from "react";
import {
View,
Text,
StyleSheet,
FlatList
} from "react-native";

import { Icon } from 'native-base'


class LikesTab extends Component {

//SETTING THE STATE MAKING AN EMPTY ARRAY WHICH WE FIL
  state = {
data: []
  };

  componentWillMount() {
this.fetchData();
  }

//Getting the data
  fetchData = async () => {
const response = await fetch("api.jsonbin.io/b/5b3a49c8a5a2f34ea6b0fbc8");
const json = await response.json();
this.setState({ data: json.results });
  };

  //var customData = require('./customData.json');


//Setting what is shown
  render() {
return (
  <View style={styles.container}>
    <FlatList
      data={this.state.data}
      keyExtractor={(x, i) => i}
      renderItem={({ item }) =>
        <Text>
          {`${item.entry.medicationCodeableConcept}`}
        </Text>}
    />
  </View>
);
  }
}
export default LikesTab;

const styles = StyleSheet.create({
container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center'
}
});

【问题讨论】:

    标签: javascript android ios json react-native


    【解决方案1】:

    我认为当你设置状态时,你设置了一个未定义的值json.results 而不是你需要这样做

    fetchData = async () => {
        const response = await fetch(
          'http://api.jsonbin.io/b/5b3a49c8a5a2f34ea6b0fbc8'
        );
    
        const json = await response.json();
        console.log(json);
        this.setState({ data: json.entry });
      };
    

    并且在渲染时使用

            <Text>{item.resource.medicationCodeableConcept.text}</Text>
    

    【讨论】:

      猜你喜欢
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 2018-03-13
      相关资源
      最近更新 更多