如何从反应获取方法获取json数据

问题描述 投票:0回答:1

我无法获取数据表单API。我尝试了不同的方式,但没有运气。这是我的代码和API。我想通过反应获取方法使用这些数据(JSON)。

// My Fetch API

fetchData() {
        fetch('MYAPI_URL')
       .then(response => response.json())
        
  // .then(parsedJSON => console.log(parsedJSON)) //prints the data on console 
//        .then(parsedJSON => console.log(parsedJSON.status)) // Not Worked
  //      .then(parsedJSON => console.log(parsedJSON.data.Products) )  // Not Worked
      .then(parsedJSON => console.log(parsedJSON.status.data) ) // not working. 

  .then(parsedJSON => parsedJSON.data.map(pro => (
            {
      ..... Other code ....
      }
      ))

   }
    
    
// below is my api data

  "status": true,
  "data": {
    "banner": "mydomain.com/img/img1.png",
    "Products": [
      {
        "CategoryCode": "03",
        "CategoryName": "Appetizers",
        "SubCategory": [
          {
            "SubCategoryCode": "038",
            "SubCategoryName": "Chicken Wings",
            "Image":     "banner": "mydomain.com/img/img1.png",
            "SubCategoryType": [
              {
                "TypeCode": "05",
                "TypeName": "Normal",
                "AdditionalPrice": "0",
                "TypeSize": [
                  {
                    "SizeCode": "17",
                    "SizeName": "6 Pcs",
                    "Description": "Oven baked hot and spicy chicken wings.",
                    "Price": "349",
                    "Toppings": [
                      
                    ]
                   }
                ]
              }
             ]
             ...............



// I want to get Products Code etc. But I can't. No idea where I am doing wrong

当我使用这个fetch api时,它正在工作。数据与上面的数据几乎相同,但这不是真正的Rest API。

json reactjs rest fetch fetch-api
1个回答
0
投票
  1. 如果您只需要接收数据而不更新React组件(不以本地状态存储),您可以查看以下答案:https://stackoverflow.com/a/44644532/1263904
  2. 否则请检查:https://stackoverflow.com/a/50980558/1263904
© www.soinside.com 2019 - 2024. All rights reserved.