Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何使用键作为子对象访问对象?-自然反应_Arrays_Json_Reactjs_Object_React Native - Fatal编程技术网

Arrays 如何使用键作为子对象访问对象?-自然反应

Arrays 如何使用键作为子对象访问对象?-自然反应,arrays,json,reactjs,object,react-native,Arrays,Json,Reactjs,Object,React Native,我正在尝试显示此JSON数据: [ { "id":"1", "imagename":"dog" }, { "id":"2", "imagename":"cat" }, { "id":"3", "imagename":"mouse" }, { "id":"4", "imagename":"deer" }, { "i

我正在尝试显示此JSON数据:

[  
   {  
      "id":"1",
      "imagename":"dog"
   },
   {  
      "id":"2",
      "imagename":"cat"
   },
   {  
      "id":"3",
      "imagename":"mouse"
   },
   {  
      "id":"4",
      "imagename":"deer"
   },
   {  
      "id":"5",
      "imagename":"shark"
   },
   {  
      "id":"6",
      "imagename":"ant"
   }
]
以下是我必须显示该数据的当前代码:

 componentDidMount(){
     fetch(`http://www.example.com/React/data.php`, {
     method: 'POST',
     headers: {
       'Accept': 'application/json',
       'Content-Type': 'application/json',
     },
   }).then((response) => response.json())
       .then((responseJson) => {

      this.data = responseJson;
      this.setState({ loading: false });


   }).catch((error) => {
     console.warn(error);
   });

}

     return(

         <View style = { styles.MainContainer }>
          <View>
           <Card>          
              <View>
              <Text>{this.data.id}</Text>
              <Text>{this.data.imagename}</Text>
             </View>
           </Card>
           </View>
         </View>
       );
最后,这里是我尝试的其余代码:

return(

     <View style = { styles.MainContainer }>
      <View>
       <Card>          
          <View key={i}>
          <Text>{item.id}</Text>
          <Text>{item.imagename}</Text>
         </View>
       </Card>
       </View>
     </View>
   );
如果有人需要查看我的
data.php

回声对象

    $dsql = "SELECT * FROM random";

$dresult = $con->query($dsql);

if ($dresult->num_rows >0) {


 while($drow[] = $dresult->fetch_assoc()) {

 $dtem = $drow;

 $djson = json_encode($dtem);


 }

} else {
}
echo $djson;
回声阵列

$dsql = "SELECT * FROM random";

$dresult = $con->query($dsql);

if ($dresult->num_rows >0) {


 while($drow = $dresult->fetch_assoc()) {

 $dtem = $drow;

 $djson = json_encode($dtem);
 echo $djson;

 }

} else {
}

我可以看到您在map函数上传递参数
I
时出现错误,请看一下这个简单的示例,了解如何使用map渲染
  • 元素

    var数据样本=[
    {“id”:“1”,“imagename”:“dog”},
    {“id”:“2”,“imagename”:“cat”},
    {“id”:“3”,“imagename”:“mouse”},
    {“id”:“4”,“imagename”:“deer”},
    {“id”:“5”,“imagename”:“shark”},
    {“id”:“6”,“imagename”:“ant”}
    ];
    常量应用=()=>(
    
      {dataSample.map((数据,i)=>{ 返回
    • {i+'-'+data.imagename}
    • })}
    ); ReactDOM.render(,document.getElementById('root'))
    
    
    您必须将索引(i变量)定义为map函数的第二个参数,尝试更改为:
    this.data=responseJson.map((item,i)=>({/…More logic here}))
    @ricardoorellana hmm,我发现:
    ReferenceError:找不到变量:我
    我也尝试过这样做:
    I:responseJson
    ,但我仍然得到了错误。我会在React Native中执行此操作吗?不,您应该通过一个有效的
    React Native
    组件来更改html元素,就像您的示例中那样。我只是想给大家展示一个关于react的map函数的工作示例。是的,我只需要问一个正确的问题。现在我只需要弄清楚如何访问我的
    response.json中的
    id
    ,我知道了,你的responseJSON叫做对象数组,因为它是由包含对象的数组组成的。当你试图用
    map
    函数阅读它时,你仍然会出错吗?哦,好吧,我从来没有理解过这个术语,现在没有任何东西都能正常工作。我只需要使用
    this.state
        $dsql = "SELECT * FROM random";
    
    $dresult = $con->query($dsql);
    
    if ($dresult->num_rows >0) {
    
    
     while($drow[] = $dresult->fetch_assoc()) {
    
     $dtem = $drow;
    
     $djson = json_encode($dtem);
    
    
     }
    
    } else {
    }
    echo $djson;
    
    $dsql = "SELECT * FROM random";
    
    $dresult = $con->query($dsql);
    
    if ($dresult->num_rows >0) {
    
    
     while($drow = $dresult->fetch_assoc()) {
    
     $dtem = $drow;
    
     $djson = json_encode($dtem);
     echo $djson;
    
     }
    
    } else {
    }