2016-03-01 71 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有什么问题。