所以我试图将这个JSON转换成JavaScript对象,但是我尝试过的所有东西都是未定义的。对象不起作用。我这里有一些代码,它是从json文件中获取对象的getStaticProps
方法
export async function getStaticProps({ params }) {
const json = await fetch(" /* LINK */ ");
const properties = await json.json();
const { starDesc, planetDesc, moonDesc, objectDesc } = properties;
return {
props: {
starDesc,
planetDesc,
moonDesc,
objectDesc,
}
};
}
下面是我的json文件的整体结构...
{
"starDesc": {
"starProperties": [{
"name": "Spectral Type",
"desc": "The spectral designation of a star."
},
{
"name": "Color",
"desc": "The color of a star's light. Bluer stars are usually hotter while redder stars are cooler."
},
....
]
},
"planetDesc": {
....
}
诸若此类
这基本上就是说,所有这些对象(starDesc
、planetDesc
、moonDesc
、objectDesc
)都是未定义的,而它们应该是新的javascript对象,对吗?我不明白我在这里做错了什么,如果有任何帮助,我将不胜感激:)
编辑:这是导出函数。这只是一个简单的测试,用于显示一些内容,但它说明starDesc.starProperties
是未定义的
export default function PropertySection(props) {
return (
<p>{props.starDesc.starProperties[0].name}</p>
)
}
转载请注明出处:http://www.intrusion-fire.net/article/20230526/1402532.html