簡體   English   中英

訪問json reactjs中的字段

[英]Access to fields in json reactjs

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,如何訪問主臨時文件?

當我編寫this.props.dates.main.temp時,我總是變得不確定,但是如果我編寫this.props.dates.name,那么在編寫this.props.date.weather [0] .description時也沒有問題。響應是不確定的。 有什么問題,我不知道...

您是指日期/日期/數據嗎? 您在說明中使用日期和日期。 我認為您應該更多地了解如何獲取/設置json數據,但這應該是您的主要問題。

它應該可以工作,也許您沒有傳遞正確的數據,我做了一個可以正常工作的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'));

好了,終於可以閱讀json的字段了,解決的方法是,在狀態下,使用datos設置一個屬性:{main {temp:0}}這樣,該屬性在開始時具有一個值。 但實際上我不明白該字段json的問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM