React Native json对象未在ListView中显示

时间:2016-09-04 15:31:00

标签: json listview react-native

我是RN的新手,我正在尝试显示一个填充了json数组数据的ListView,如果我使用一个简单的数组,那么我可以看到数据但是当我使用json数组时,如代码所示当我运行模拟器时,我看不到任何填充物。在下面的示例中是否有一种特定的方法来处理json以在ListView中显示它:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="movieApp" ng-controller="homeController">

  <!-- FOREACH the movies -->
  <div ng-repeat="movie in movies | orderBy : order">
    <div class="movie-wrapper">
      <h1>{{movie.name}}</h1>
      <p>IMDB: <a href="{{movie.imdb_url}}" target="_blank">IMDB</a>
      </p>
    </div>
  </div>

  <!-- ADD a new movie to the array -->
  <div>

    <h3>Add new movie:</h3>
    <form name="movieForm" ng-submit="addMovie(movieInfo)">
      <div class="form-group new-movie">
        <label for="Title">Title:</label>
        <input type="text" class="form-control" name="title" ng-model="movieInfo.title">
      </div>
      <div class="form-group new-movie">
        <label for="IMDB">IMDB:</label>
        <input type="text" class="form-control" name="imdb" ng-model="movieInfo.imdb">
      </div>

      <button type="submit" class="btn btn-primary">Add movie</button>
    </form>
  </div>

</div>

1 个答案:

答案 0 :(得分:3)

如果我引用the docs,看起来data应该是数组而不是对象。你可以试试以下吗?

let data = [
  {"vehicle_make": "Toyota", 
  "vehicle_model": "Camry", 
  "vehicle_year": "2015", 
  "vehicle_price_range": "$$ (10-30)", 
  "car_id": 1127, 
  "vehicle_color": "Black", 
  "vehicle_car_accidents": "No", 
  "vehicle_no_owners": "1", 
  "car_vehicle_location": "Lot", 
  "vehicle_city_location": "L.A", 
  "vehicle_mileage": 10864, 
  "vehicle_vin": 1234, 
  "vehicle_off_lease": "Yes", 
  "vehicle_state": "CA"
}];

编辑: 你可以改为替换

dataSource: ds.cloneWithRows(data)

dataSource: ds.cloneWithRows(data.Vehicles[0])

但我建议你尽可能地平整你的数据。

编辑2: render方法也需要修改。尝试将renderRow更改为以下内容:

renderRow={(rowData) => <Text>{rowData.vehicle_make}</Text>}