【问题标题】:Access to fields in json reactjs访问json reactjs中的字段
【发布时间】:2016-03-01 00:45:27
【问题描述】:
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 ,我如何访问 main temp??

当我写 this.props.dates.main.temp 我总是得到 undefined ,但是如果我写 this.props.dates.name ,我没有问题,当我写 this.props.date.weather[0 ].description 响应未定义。 什么问题,我不知道...

【问题讨论】:

    标签: javascript json reactjs


    【解决方案1】:

    您是指日期/日期/数据吗?您在描述中使用日期和日期。我认为你应该更多地展示你如何获取/设置你的 json 数据,但这应该是你的主要问题。

    【讨论】:

      【解决方案2】:

      它应该可以工作,也许你没有传递正确的数据,我做了一个可以工作的代码笔,试着检查一下。

      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'));
      

      【讨论】:

      • 我认为编辑第一篇文章会更好。
      【解决方案3】:

      最后我可以读取 json 的字段,解决方案是,在 state 中,使用 datos 创建一个属性:{ main { temp: 0}} 有了这个,这个属性一开始就有一个值。 但我真的不明白那个字段 json 有什么问题。

      【讨论】:

        猜你喜欢
        • 2018-10-31
        • 2021-03-04
        • 2020-09-29
        • 2016-11-09
        • 1970-01-01
        • 1970-01-01
        • 2015-12-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多