2017-03-16 23 views
0

私は反応ネイティブで作業している問題に遭遇しました。私は大きなJSONオブジェクトを解析し、その内部にネストされた配列を反復処理する必要があります。私がする必要があるのは、day [0]の各アイテムオブジェクトに3つの値を出力することだけです。反応したネイティブでJSONを繰り返し処理する

マイコード:

import React, { Component, PropTypes } from 'react'; 
import { View, Text, ListView, StyleSheet, TouchableHighlight} from 'react-native'; 
import Header from '../Components/Header'; 
import Api from '../Utility/Api'; 


export default class CalendarPage extends Component { 

constructor(props) { 
    super(props); 
    const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); 
    this.state = { 
     dataSource: ds.cloneWithRows(
     fetch('https://s3.amazonaws.com/cbu-rec-center-app/credentials/schedule.json') 
    ) 
    }; 
    } 
    componentWillMount(){ 
     Api.getDates().then((res) => { 
      this.setState({ 
        //I need to have these three output, but for all items in day[0] 
        EventName: res.days[0].items[0].summary, 
        EventDate: res.days[0].items[0].start.dateTime, 
        EventLocation: res.days[0].items[0].description 
      }) 
     }) 
    } 

     render() { 
     return (
      <View style={{flex: 1}}> 
      <Header pageName="Calendar" navigator={this.props.navigator}/> 
      <View style = {{flex:9}}> 
       <Text> {this.state.EventName} </Text> 
       <Text> {this.state.EventDate} </Text> 
       <Text> {this.state.EventLocation} </Text> 
      </View> 
      </View> 
     ); 
     } 
} 

JSON

"days": [{ 
    "date": "2017-03-06", 
    "hours": { 
     "open": "06:00", 
     "close": "12:00" 
    }, 
    "items": [{ 
      "kind": "calendar#event", 
      "etag": "\"2977101842476000\"", 
      "id": "fhq5hof67nvqhj85qm65t1n3e4", 
      "status": "confirmed", 
      "htmlLink": "https://www.google.com/calendar/event?eid=ZmhxNWhvZjY3bnZxaGo4NXFtNjV0MW4zZTQgY2J1cmVjcmVhdGlvbmNlbnRlckBt", 
      "created": "2017-03-03T14:22:01.000Z", 
      "updated": "2017-03-03T14:22:01.238Z", 
      "summary": "Women's Volleyball", 
      "description": "West Court", 
      "creator": { 
      "email": "[email protected]", 
      "displayName": "Cbu RecreationCenter", 
      "self": true 
      }, 
      "organizer": { 
      "email": "[email protected]", 
      "displayName": "Cbu RecreationCenter", 
      "self": true 
      }, 
      "start": { 
      "dateTime": "2017-03-06T15:30:00-08:00" 
      }, 
      "end": { 
      "dateTime": "2017-03-06T16:30:00-08:00" 
      }, 
      "iCalUID": "[email protected]", 
      "sequence": 0, 
      "reminders": { 
      "useDefault": true 
      }, 
      "type": "event" 
     }, 
     { 
      "kind": "calendar#event", 
      "etag": "\"2976616094232000\"", 
      "id": "4tnn4gn0gstndi5idrqjsg7elo_20170306T200000Z", 
      "status": "confirmed", 
      "htmlLink": "https://www.google.com/calendar/event?eid=NHRubjRnbjBnc3RuZGk1aWRycWpzZzdlbG9fMjAxNzAzMDZUMjAwMDAwWiBrbTVyM2dycW1qbjZiMTQ2MWk2aXBjMjJhc0Bn", 
      "created": "2017-02-28T14:24:06.000Z", 
      "updated": "2017-02-28T18:54:07.116Z", 
      "summary": "Boxing Boot Camp", 
      "creator": { 
      "email": "[email protected]", 
      "displayName": "Cbu RecreationCenter" 
      }, 
      "organizer": { 
      "email": "[email protected]", 
      "displayName": "Group X Calendar", 
      "self": true 
      }, 
      "start": { 
      "dateTime": "2017-03-06T12:00:00-08:00" 
      }, 
      "end": { 
      "dateTime": "2017-03-06T12:45:00-08:00" 
      }, 
      "recurringEventId": "4tnn4gn0gstndi5idrqjsg7elo", 
      "originalStartTime": { 
      "dateTime": "2017-03-06T12:00:00-08:00" 
      }, 
      "iCalUID": "[email protected]", 
      "sequence": 0, 
      "reminders": { 
      "useDefault": true 
      }, 
      "type": "class" 
     }, 
     { 
      "kind": "calendar#event", 
      "etag": "\"2967485504076000\"", 
      "id": "m533eg9bu5o4meinuu7pvfoge4_20170306T210000Z", 
      "status": "confirmed", 
      "htmlLink": "https://www.google.com/calendar/event?eid=bTUzM2VnOWJ1NW80bWVpbnV1N3B2Zm9nZTRfMjAxNzAzMDZUMjEwMDAwWiBrbTVyM2dycW1qbjZiMTQ2MWk2aXBjMjJhc0Bn", 
      "created": "2017-01-06T22:45:52.000Z", 
      "updated": "2017-01-06T22:45:52.038Z", 
      "summary": "Women on Weights", 
      "description": "This is a weight lifting class designed to empower women to lift free weights in a group setting . The goals of WOW are to teach proper form, assist women in improving their posture, increasing their strength, and muscle pairing. ", 
      "location": "Group X Room", 
      "creator": { 
      "email": "[email protected]", 
      "displayName": "Cbu RecreationCenter" 
      }, 
      "organizer": { 
      "email": "[email protected]", 
      "displayName": "Group X Calendar", 
      "self": true 
      }, 
      "start": { 
      "dateTime": "2017-03-06T13:00:00-08:00", 
      "timeZone": "America/Los_Angeles" 
      }, 
      "end": { 
      "dateTime": "2017-03-06T14:00:00-08:00", 
      "timeZone": "America/Los_Angeles" 
      }, 
      "recurringEventId": "m533eg9bu5o4meinuu7pvfoge4", 
      "originalStartTime": { 
      "dateTime": "2017-03-06T13:00:00-08:00", 
      "timeZone": "America/Los_Angeles" 
      }, 
      "iCalUID": "[email protected]", 
      "sequence": 0, 
      "reminders": { 
      "useDefault": true 
      }, 
      "type": "class" 
     }, 

答えて

3

あなたが.map()を使用してそれを行うことができます。

<View> 
 
    { res.days[0].items.map((item) => (
 
    <View> 
 
     <Text>{item.summary}</Text> 
 
     <Text>{item.start.dateTime}</Text> 
 
     <Text>{item.description}</Text> 
 
    </View> 
 
))} 
 
</View>

データが空でないことを確認してください。<Text>ブロック

+0

resはグローバル変数ではありません。申し訳ありませんが、私は新しい反応です。 – JerryVonJingles

+0

'非同期コンポーネントwillMount(){this.setState({データ:Api.getDates()}}' – dhorelik

+0

を実行すると、 'this.state.data'にすべての項目があります。 APIリクエストであるため、データが取得されるまでローダをレンダリングする必要があります – dhorelik

関連する問題