admin管理员组

文章数量:1345415

I use ReactJS to get data from a JSON file

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Demo Fetch</title>
    <link rel="stylesheet" href="style.css" />
    <script src=".13.3/react.js"></script>
    <script src=".13.3/JSXTransformer.js"></script>
    <script src="js/jquery-2.1.4.min.js"></script>
</head>
<body>
    <div id="content"></div>
    <script type="text/jsx">
        var DataBlock = React.createClass({
            getInitialState:function(){
                return {data:[]};
            },
            ponentDidMount:function() {
                var a=this;
                $.getJSON(this.props.url, function(data) {
                    a.setState({data:data})
                });
            },
            render: function() {
                console.log(this.state);
                return (
                        <div>
                            <h1>Sample data block</h1>
                                <h3>{this.state.data.movies[0].title}</h3>
                        </div>
                );
            }
        });
        React.render(
                <DataBlock url="small_data.json"/>,
                document.getElementById('content')
        );
    </script>
</body>
</html>

And this is JSON file small_data.json

{
  "movies": [
    {
      "abridged_cast": [
        {
          "characters": [
            "Dominic Toretto"
          ],
          "id": "162652472",
          "name": "Vin Diesel"
        },
        {
          "characters": [
            "Brian O'Conner"
          ],
          "id": "162654234",
          "name": "Paul Walker"
        }
      ],
      "synopsis": "Continuing the global exploits in the unstoppable franchise built on speed, Vin Diesel, Paul Walker and Dwayne Johnson lead the returning cast of Fast & Furious 7. James Wan directs this chapter of the hugely successful series that also weles back favorites Michelle Rodriguez, Jordana Brewster, Tyrese Gibson, Chris \"Ludacris\" Bridges, Elsa Pataky and Lucas Black. They are joined by international action stars new to the franchise including Jason Statham, Djimon Hounsou, Tony Jaa, Ronda Rousey and Kurt Russell.",
      "title": "Furious 7",
      "year": 2015
    }
  ],
  "runtime": 140
}

Neither title nor anything else is displayed. But when I try to display a non-array object in JSON file, like runtime, I just use {this.state.data.runtime}, it shows as expected, but how can I get access to movies array? I thought I use proper syntax to retrive the array. If it's not like this, what is the true one? How can I get data from objects in array in JSON and store in variables in ReactJS?

I use ReactJS to get data from a JSON file

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Demo Fetch</title>
    <link rel="stylesheet" href="style.css" />
    <script src="https://cdnjs.cloudflare./ajax/libs/react/0.13.3/react.js"></script>
    <script src="https://cdnjs.cloudflare./ajax/libs/react/0.13.3/JSXTransformer.js"></script>
    <script src="js/jquery-2.1.4.min.js"></script>
</head>
<body>
    <div id="content"></div>
    <script type="text/jsx">
        var DataBlock = React.createClass({
            getInitialState:function(){
                return {data:[]};
            },
            ponentDidMount:function() {
                var a=this;
                $.getJSON(this.props.url, function(data) {
                    a.setState({data:data})
                });
            },
            render: function() {
                console.log(this.state);
                return (
                        <div>
                            <h1>Sample data block</h1>
                                <h3>{this.state.data.movies[0].title}</h3>
                        </div>
                );
            }
        });
        React.render(
                <DataBlock url="small_data.json"/>,
                document.getElementById('content')
        );
    </script>
</body>
</html>

And this is JSON file small_data.json

{
  "movies": [
    {
      "abridged_cast": [
        {
          "characters": [
            "Dominic Toretto"
          ],
          "id": "162652472",
          "name": "Vin Diesel"
        },
        {
          "characters": [
            "Brian O'Conner"
          ],
          "id": "162654234",
          "name": "Paul Walker"
        }
      ],
      "synopsis": "Continuing the global exploits in the unstoppable franchise built on speed, Vin Diesel, Paul Walker and Dwayne Johnson lead the returning cast of Fast & Furious 7. James Wan directs this chapter of the hugely successful series that also weles back favorites Michelle Rodriguez, Jordana Brewster, Tyrese Gibson, Chris \"Ludacris\" Bridges, Elsa Pataky and Lucas Black. They are joined by international action stars new to the franchise including Jason Statham, Djimon Hounsou, Tony Jaa, Ronda Rousey and Kurt Russell.",
      "title": "Furious 7",
      "year": 2015
    }
  ],
  "runtime": 140
}

Neither title nor anything else is displayed. But when I try to display a non-array object in JSON file, like runtime, I just use {this.state.data.runtime}, it shows as expected, but how can I get access to movies array? I thought I use proper syntax to retrive the array. If it's not like this, what is the true one? How can I get data from objects in array in JSON and store in variables in ReactJS?

Share Improve this question edited Jan 31, 2016 at 3:48 Dmitry Shvedov 3,3164 gold badges40 silver badges56 bronze badges asked Sep 28, 2015 at 8:47 necrofacenecroface 3,46513 gold badges48 silver badges71 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

I think you're trying to achieve something like this, right: http://codepen.io/zvona/pen/BoQVoj?editors=001

return (
  <div>
    <h1>Sample data block</h1>
    {this.state.data.movies.map(function(movie, i) {
      return <h3 key={'movie-'+ i}>{movie.title}</h3>
    })}
  </div>
);

It loops through the movies array and .map() returns the value for each movie to be displayed.

本文标签: javascriptGet access to array in JSON by ReactJSStack Overflow