javascript - 在 React 中映射本地 .json 数据

标签 javascript arrays json reactjs

我有一个格式如下的本地 .json 文件

    {
  "results": [
    {
      "id": 1,
      "title": "2 bedroom apartment to rent",
      "location": "30 South Colonnade London E14 5EZ",
      "description": "The building offers a communal lifestyle which consists of a large lounge area with dining room, working space, TV lounge, gym, stylish meeting rooms, free table tennis, free WIFI and a spacious communal roof terrace. Local transport includes Canning Town DLR and Jubilee Line (0.1 miles). Argo Apartments is managed by Grainger plc, one of the UK's leading professional landlords with over 100 years experience.",
      "price": "1,800",
      "beds": 2,
      "bathrooms": 1,
      "landlord": "Hamptons International Lettings",
      "images": ["image1", "image2"]
    },
    {
      "id": 2,
      "title": "2 bedroom flat to rent",
      "location": "Textile Building, Chatham Place, Hackney, London, E9",
      "description": "SHORT LET - Forming part of the new eagerly awaited Textile Building in Hackney Central E8, this stunning two double bedroom property has been finished to the highest standard, featuring two modern fitted bathrooms (one en-suite), two equal double bedrooms, spacious open plan reception with brand new integrated kitchen, exposed brickwork and communal underground bike storage.",
      "price": "2,400",
      "beds": 2,
      "bathrooms": 1,
      "landlord": "Stirling Ackroyd, Hackney",
      "images": ["image1", "image2"]
    },
    {
      "id": 3,
      "title": "3 bedroom apartment to rent",
      "location": "Sixth Floor Tower Building 11 York Road London SE1 7NX",
      "description": "A fantastic three bedroom apartment set in this popular development close to Vauxhall and Stockwell tube stations. The apartment is neutrally decorated throughout and is available either furnished or unfurnished. Residents will enjoy the plethora of shops, bars and restaurants and leisure activities in this popular part of London as well as the excellent commuter links towards Central London.",
      "price": "2,050",
      "beds": 3,
      "bathrooms": 2,
      "landlord": "Regent Letting and Property Management",
      "images": ["image1", "image2"]
    }
  ],
  "newResults" : [
      {
      "id": 4,
      "title": "2 bedroom flat to rent",
      "location": "Conway Street, Fitzrovia, W1T",
      "description": "Complete is delighted to market this newly renovated period property situated on the corner of Fitzroy Square in the heart of Fitzrovia, W1. The property is located on the ground floor and comes with 2 double bedrooms, shower room, open plan living and kitchen area. The kitchen is fully fitted with Siemens appliances and Duravit fittings in the bathroom. This apartment has high ceiling and retains its original period features. Located on the corner of this garden square this development was built in the 18th Century and benefits from being in the heart of vibrant London whilst also being a quiet residential area.",
      "price": "1,500",
      "beds": 2,
      "bathrooms": 1,
      "landlord": "Barnard Marcus Lettings",
      "images": ["image1", "image2"]
    }  
  ]
}

我希望能够调用映射到组件的相关信息,如下所示

class ResultsLists extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      results: resultsData
    }
  }

  render() {
    const results = this.state.results.results.map((result) =>
      <li>{result}</li>
    );

    return (
      <div className="rst-List">
        <ul className="rst-List_Items">
          {results}
        </ul>
      </div>
    )
  }
}

我似乎无法让它正常工作,并且收到错误消息 - 对象作为 React 子对象无效。关于这不起作用的原因似乎有多种多样,想知道是否有人可以进行完整性检查。

最佳答案

您正在映射 results,但随后您将整个 result 对象放入 JSX。

这是一个如何映射数据的例子:

const someData = [{ name: "Colin" }, { name: "Ricardo" }];

const App = () => {
  const temp = someData.map(o => <li>{o.name}</li>);

  return (
    <div>
      <ul>{temp}</ul>
    </div>
  );
};

请注意,我们有 {o.name} 而不仅仅是 {o}

工作示例 here .

所以在你的情况下,你需要这样的东西:

const results = this.state.results.results.map((result) =>
  <li>{result.title}</li>
);

关于javascript - 在 React 中映射本地 .json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50100079/

相关文章:

json - NodeJs Express 将 json 数据发送到主干

javascript - Bootstrap + 2个版本的jquery

javascript - puppeteer 如何返回 page.on 响应值

javascript - 当事件处理程序附加到父项时,防止在某个子项之后单击

javascript - 如何同时合并和平展数组

ios - 如何将另一个数组中的数组存储到 Swift 中从 JSON 传递的全局变量?

javascript - JSON.parse 出现问题,不知道为什么没有解析所有内容

Javascript setInterval 连续多行

java - 将对象转换为泛型类型失败

r - 以 R 格式导出 JSON 数据框