2016-03-01 72 views
0
base: "cmc stations" 
clouds: {all: 76} 
cod: 200 
coord: {lon: 13.37, lat: 52.52} 
dt: 1456791041 
id: 2822234 
main: {temp: 0.17, pressure: 1028.91, humidity: 77, temp_min: 0.17, temp_max: 0.17, sea_level: 1035.1,…} 
name: "Tiergarten" 
sys: {message: 0.003, country: "DE", sunrise: 1456811534, sunset: 1456850772} 
weather: [{id: 803, main: "Clouds", description: "broken clouds", icon: "04n"}] 
wind: {speed: 5.21, deg: 48.0012} 

大家好,請用這個json和reactjs,如何我可以訪問主temp?訪問json中的字段reactjs

當我寫這個.props.dates.main.temp我總是得到未定義,但如果我寫這個.props.dates.name,我沒有問題,當我寫this.props.date.weather [ 0] .description的響應是未定義的。 什麼問題,我不知道...

回答

0

你是指日期/日期/數據?您在描述中使用日期和日期。我認爲你應該多展示一下你如何獲取/設置你的json數據,但這應該是你的主要問題。

+0

我寫我的代碼,所以你可以看到它。 – Sergio

0

它應該工作,也許你沒有通過正確的數據,我做了一個正在工作的codepen,嘗試檢查出來。

http://codepen.io/philolo1/pen/WwbObQ

var Example = React.createClass({ 
    getDefaultProps: function() { 
    return { 
     base: "cmc stations", 
    clouds: {all: 76}, 
    cod: 200, 
    coord: {lon: 13.37, lat: 52.52}, 
    dt: 1456791041, 
    id: 2822234, 
    main: {temp: 0.17, pressure: 1028.91, humidity: 77, temp_min: 0.17, temp_max: 0.17, sea_level: 1035.1}, 
    name: "Tiergarten", 
    sys: {message: 0.003, country: "DE", sunrise: 1456811534, sunset: 1456850772}, 
    weather: [{id: 803, main: "Clouds", description: "broken clouds", icon: "04n"}], 
    wind: {speed: 5.21, deg: 48.0012} 
    } 
    }, 

    styler: { 
     width: "100px", 
    height: "100px", 
    mozBorderRadius: "50%", 
    webkitBorderRadius: "50%", 
    borderRadius: "50%", 
    background: "red" 
    }, 

    render: function() { 
     return (
      <div> {this.props.main.temp}</div> 
     ); 
    } 
}); 

React.render(
    <Example />, 
    document.getElementById('mount-point')); 
+0

我寫我的代碼,以便您可以看到它。 – Sergio

+0

你應該編輯你的問題不寫回答 –

+0

我認爲這是更好的編輯第一篇文章的迴應。 – Sergio

0

好吧我終於可以閱讀JSON的領域,解決的辦法是,在狀態,使一個屬性與DATOS:{主要{溫度:0}} 就這樣,這個屬性有一開始就是一個值。 但我真的不明白這個字段json有什麼問題。