【问题标题】:ReactJs : how to extract Json data from an array to display it on a div of react CarouselReactJs:如何从数组中提取 Json 数据以将其显示在 React Carousel 的 div 上
【发布时间】:2018-05-20 13:09:01
【问题描述】:

我在提取来自 API 的 Json 数据时遇到问题。

请帮我改正我的错误。

JSON

[  
   {  
      "flagshipId":"18",
      "BanquetValues":"<p>xzxzxczx<\/p>\n",
      "FloorPlan":"[{\"id\":1,\"fileName\":\"megarugas-15243406450731525866511-1.jpg\",\"ifActive\":\"1\"},{\"id\":2,\"fileName\":\"megarugas-15243406467351525866513-2.jpg\",\"ifActive\":\"1\"},{\"id\":3,\"fileName\":\"megarugas-15244876214221526013635-3.jpg\",\"ifActive\":\"1\"}]",
      "ChildDescription":"[{\"id\":1,\"childName\":\"Ceremony 1 @ Megarugas\",\"description\":\"xczxcxvx\"}]",
      "RestaurantId":"695"
   }
]

我想将 FloorPlan 数组中的文件名显示到我的轮播中。

JSX

render()
    {
        var banquetImg = this.props.IMG_BASE + this.props.RESTAURANT_BANNER_PATH
      return (
        <div className="photosSection">
          {
            this.props.banquetImageList.length != 0
            ?
              <div className="body">
                <div className="row">
           <Carousel showArrows={true} >
                 {this.props.banquetImageList.map((row, i) =>
                       <div key={row.RestaurantAttachmentId} className={"row"}>
                          <img src={banquetImg + row.FileName} key={row.RestaurantAttachmentId}/>
                             <p className="get-final-price">Get Final Price</p>
                       </div>
                     )}
                  </Carousel>   
                 </div>
              </div>
              :
                ""
          } 
        </div>
    );
  }

【问题讨论】:

  • 如果json_object[0].FloorPlan是序列化对象,可以使用JSON.parse(json_string_here)将其转换为json。
  • @Al.G.感谢您的答复。你能帮我这样做吗?我无法修复它。
  • 是的,当然。写JSON.parse(json_object[0].FloorPlan),你会得到所需的数组。
  • @Al.G.它返回 Uncaught TypeError: Cannot read property 'FloorPlan' of undefined

标签: arrays json reactjs arraylist carousel


【解决方案1】:

我尝试过像@AI.G 这样的方法。这是我的代码:

let json_data = [
   {  
      "flagshipId":"18",
      "BanquetValues":"<p>xzxzxczx<\/p>\n",
      "FloorPlan":"[{\"id\":1,\"fileName\":\"megarugas-15243406450731525866511-1.jpg\",\"ifActive\":\"1\"},{\"id\":2,\"fileName\":\"megarugas-15243406467351525866513-2.jpg\",\"ifActive\":\"1\"},{\"id\":3,\"fileName\":\"megarugas-15244876214221526013635-3.jpg\",\"ifActive\":\"1\"}]",
      "ChildDescription":"[{\"id\":1,\"childName\":\"Ceremony 1 @ Megarugas\",\"description\":\"xczxcxvx\"}]",
      "RestaurantId":"695"
   }
];

floor_plan = JSON.parse(json_data[0]['FloorPlan']);

console.log(floor_plan);

这就是我从终端得到的(MacOS 10.13.4,NodeJS v8.11.1):

$ node test.js 
[ { id: 1,
    fileName: 'megarugas-15243406450731525866511-1.jpg',
    ifActive: '1' },
  { id: 2,
    fileName: 'megarugas-15243406467351525866513-2.jpg',
    ifActive: '1' },
  { id: 3,
    fileName: 'megarugas-15244876214221526013635-3.jpg',
    ifActive: '1' } ]

您可以从 floor_plan(当前是一个数组)中获取每个元素。

这是你的目标吗?

【讨论】:

  • 嗨,console.log(this.props.banquetFlagship[0]['FloorPlan'], "uspArray") 我得到了 Uncaught TypeError: Cannot read property 'FloorPlan' of undefined
  • @TanmoySarkar 这是反应属性的错误。你的问题是关于 json 解析,而不是反应。请不要一次问两个不同的问题。这是有关从序列化数组中提取元素的问题的正确答案。如果您对如何在反应中呈现值有疑问,请再问一个问题,如果它解决了提取值的问题,则将此答案标记为已接受。
  • @TanmoySarkar 你能把 this.props.banquetFlagship 登录到这里吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
  • 2012-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
相关资源
最近更新 更多