如何将JSON数据加载到A-Frame组件中?

ngo*_*vin 5 aframe

将自定义JSON文件作为数据加载到A框架组件中的最佳方法是什么?例如,JSON文件可能包含点的坐标。我想将文件作为资产加载,并在组件中使用已解析的json对象。

{"coordinates": [{"x": 0, "y": 1, "z": 2}, // ...]}
Run Code Online (Sandbox Code Playgroud)

ngo*_*vin 4

您可以在架构中定义您自己的属性类型,以您希望的方式解析数据

要从组件解析 JSON,请创建一个parse执行以下操作的函数JSON.parse

AFRAME.registerComponent('foo', {
  schema: {
    jsonData: {
      parse: JSON.parse,
      stringify: JSON.stringify
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

然后使用该组件:

el.setAttribute('foo', 'jsonData', yourJsonData);
Run Code Online (Sandbox Code Playgroud)

或者:

<a-entity foo='jsonData: {"coordinates": [{"x": 0, "y": 1, "z": 2}]}'></a-entity>
Run Code Online (Sandbox Code Playgroud)