React Native,限制外部文档的JSON输出

问题描述 投票:1回答:1

我正在尝试将JSON内容输出到平板列表中,如反应原生文档中所述。将内容输出到页面不是问题,我正面临着,我正在尝试限制从每个JSON字段输出的数据量。

例如,有4种状态,身体,精神,情感和精神。我正在尝试输出第一个元素,所以在这种情况下是物理,或[0]。

我有以下代码将输出所有4个值,我无法将值限制为第一个元素:

<FlatList
data={this.state.dimensionJson}
renderItem={({item}) => <Text style={[styles.dimensionTitle, { color: progress[3] }]}>{item.type}</Text>}
keyExtractor={({id}, index) => id}
/> 

检索JSON数据的代码如下:

componentDidMount(){
return fetch(url,{
  method: 'GET',
  headers: {
    Accept: 'applications/json',
  },
},
)
.then((response) => response.json())
.then((responseJson) => {
  this.setState({
    isLoading: false,
    titleData: responseJson.title,
    fullJSON: responseJson,
    dimensionJson: responseJson.dimensions,

  }, function() {



    //Potentially write if function in here for limiting output
  });
})
.catch((error) =>{
  console.error(error)
})}

最后我的JSON看起来像这样:

{
  "description": "Begin by identifying  the dimension of energy you would like to address. Your scores can guide the way.",
  "title": "Choose a Dimension",
  "dimensions": [
    {
      "id": "0",
      "type": "Physical",
      "desc": "Physical energy is the quantity of  energy. This dimension shapes our sustainability and long-term productivity."
     },
    {
      "id": "1",
      "type": "Mental",
       "desc": "Mental energy is the focus of our energy. It influences our 
concentration, control of attention, and the likelihood of making mistakes."
    },
    {
      "id": "2",
      "type": "Emotional",
      "desc": "Emotional energy is the quality of our energy. It affects how 
resilient we are, especially when faced with complexity."
    },
    {
      "id": "3",
      "type": "Spiritual",
      "desc": "Spiritual energy is the energy we derive from serving a  
greater purpose. It inspires us and answers the question ‘Why do I get out 
of bed each morning?"
    }
  ]
 }
android json react-native react-native-flatlist
1个回答
0
投票

尝试切片数据

 <FlatList
                        data={this.state.dimensionJson.slice(0,1)}
                        renderItem={({item}) => <Text style={[styles.dimensionTitle, { color: progress[3] }]}>{item.type}</Text>}
                        keyExtractor={({id}, index) => id}
                        /> 
© www.soinside.com 2019 - 2024. All rights reserved.