文本文件中的Javascript访问密钥
我接到这个电话:文本文件中的Javascript访问密钥,javascript,reactjs,Javascript,Reactjs,我接到这个电话: async getData(budget) { const res = await fetch("/json/sample.txt"); const data = await res.text(); return this.setState({ data }); } 我的sample.json的格式如下: {(150, 'Points'): {'0': {'name': 'Matheus', 'slug': 'matheus
async getData(budget) {
const res = await fetch("/json/sample.txt");
const data = await res.text();
return this.setState({ data });
}
我的sample.json的格式如下:
{(150, 'Points'): {'0': {'name': 'Matheus', 'slug': 'matheus-galdezani',...}
如何使用{'0':{'name':'Matheus','slug':'Matheus galdezani',…}
作为键访问{'0':{'name':'Matheus','slug',…}
呢?我建议使用(150,'Points')
这是否有效JavaScript
?
[更改]
[Do not use]
{(150, 'Points'): {'0': {'name': 'Matheus', 'slug': 'matheus-galdezani',...}
[Use]
{'150,Points': {'0': {'name': 'Matheus', 'slug': 'matheus-galdezani',...}
你可以按“150分”键
您可以使用javascript拆分从“150,Points”到[“150”,“Points”]
[Example]
const arr = '150,Points'.split(',') // ['150', 'Points'];
const num = Number(arr[0]); // 150
const str = arr[1]; // 'Points'
这不是有效的JSON,因为键没有被双引号包围,也没有字符串。
{(150,'Points'):
也无效-JSON中对象文本的第一部分必须是键,而不是明显使用逗号运算符(这在JS中是允许的,但在JSON中不是)如果它是txt呢?那么你应该修复生成文本的任何东西,因为它现在已经严重损坏了。让它用JSON生成。