使用Typescript和JSON动态生成的键

使用Typescript和JSON动态生成的键,json,typescript,Json,Typescript,所以我正在使用Typescript和React native开发应用程序。我有一个json文件,其中包含一些扑克手的信息,我需要从我的应用程序访问这些信息 JSON如下所示: { "22": [ [ 0, 20 ] ], "32o": [ [ 0, 20 ] ], "32s": [ [ 0, 20

所以我正在使用Typescript和React native开发应用程序。我有一个json文件,其中包含一些扑克手的信息,我需要从我的应用程序访问这些信息

JSON如下所示:

{
  "22": [
    [
      0,
      20
    ]
  ],
  "32o": [
    [
      0,
      20
    ]
  ],
  "32s": [
    [
      0,
      20
    ]
  ],
  "33": [
    [
      0,
      20
    ]
  ],
pdata['AA'][0][1]
import * as pdata from './push.json';
import { Convert } from './Convert'
    
const pushHands = Convert.toHands(JSON.stringify(pdata));
var ch = '42o';
console.log(pushHands['default'][ch][0][1]);
我正在使用导入json数据

import * as pdata from './push.json';
可以通过以下方式访问数据:

{
  "22": [
    [
      0,
      20
    ]
  ],
  "32o": [
    [
      0,
      20
    ]
  ],
  "32s": [
    [
      0,
      20
    ]
  ],
  "33": [
    [
      0,
      20
    ]
  ],
pdata['AA'][0][1]
import * as pdata from './push.json';
import { Convert } from './Convert'
    
const pushHands = Convert.toHands(JSON.stringify(pdata));
var ch = '42o';
console.log(pushHands['default'][ch][0][1]);
但是,如果我尝试使用这样的动态创建的键访问json

currentHand = '32o';
console.log(pdata[currentHand][0][1]);
“元素隐式地具有'any'类型,因为'string'类型的表达式不能用于索引类型'…”时出错,在类型“{”22:number[]];…”上找不到具有'string'类型参数的索引签名

由于大量的键,硬编码所有不同的可能性并不是真正可行的。
我一直在搜索答案,但没有找到任何有效的方法。

既然您声明
currentHand
而不使用
const
,Typescript希望
currentHand
变量值可以/将要更改,并且它假定
currentHand
类型是
字符串

现在,如果您使用
const
声明它:

import data from "./push.json";

const currentHand = "22";
console.log(data[currentHand][0][1]);
currentHand
的类型将是
“22”
,而不是
字符串
,错误将不再出现

由于大量的键,硬编码所有不同的可能性是不可行的。我一直在寻找答案,但没有找到任何有效的方法

试试这个

type DataKey = keyof typeof data; // DataKey type is "22" | 32o" | "32s" | ...

const currentHand: DataKey = "22";
// or
const currentHand = "22" as DataKey;

console.log(data[currentHand][0][1]);
替代方法:

import data from "./push.json";

const dynamicData = <Data>(<any>data);

type Data = {
  [key: string]: [number[]];
  // or if value structure is always the same
  [key: string]: [[number, number]];
};


let currentHand = "22";
console.log(dynamicData[currentHand][0][1]); 
从“/push.json”导入数据;
常量dynamicData=(数据);
类型数据={
[键:字符串]:[number[];
//或者如果价值结构总是相同的
[键:字符串]:[[number,number]];
};
让currentHand=“22”;
log(dynamicata[currentHand][0][1]);

由于您在声明
currentHand
时没有
const
,Typescript希望
currentHand
变量值可以/将被更改,并且它假定
currentHand
类型为
字符串

现在,如果您使用
const
声明它:

import data from "./push.json";

const currentHand = "22";
console.log(data[currentHand][0][1]);
currentHand
的类型将是
“22”
,而不是
字符串
,错误将不再出现

由于大量的键,硬编码所有不同的可能性是不可行的。我一直在寻找答案,但没有找到任何有效的方法

试试这个

type DataKey = keyof typeof data; // DataKey type is "22" | 32o" | "32s" | ...

const currentHand: DataKey = "22";
// or
const currentHand = "22" as DataKey;

console.log(data[currentHand][0][1]);
替代方法:

import data from "./push.json";

const dynamicData = <Data>(<any>data);

type Data = {
  [key: string]: [number[]];
  // or if value structure is always the same
  [key: string]: [[number, number]];
};


let currentHand = "22";
console.log(dynamicData[currentHand][0][1]); 
从“/push.json”导入数据;
常量dynamicData=(数据);
类型数据={
[键:字符串]:[number[];
//或者如果价值结构总是相同的
[键:字符串]:[[number,number]];
};
让currentHand=“22”;
log(dynamicata[currentHand][0][1]);

我找到了解决这个问题的方法。 通过将生成的转换器保存在Convert.ts中,可以如下方式访问JSON数据:

{
  "22": [
    [
      0,
      20
    ]
  ],
  "32o": [
    [
      0,
      20
    ]
  ],
  "32s": [
    [
      0,
      20
    ]
  ],
  "33": [
    [
      0,
      20
    ]
  ],
pdata['AA'][0][1]
import * as pdata from './push.json';
import { Convert } from './Convert'
    
const pushHands = Convert.toHands(JSON.stringify(pdata));
var ch = '42o';
console.log(pushHands['default'][ch][0][1]);

我找到了解决这个问题的方法。 通过将生成的转换器保存在Convert.ts中,可以如下方式访问JSON数据:

{
  "22": [
    [
      0,
      20
    ]
  ],
  "32o": [
    [
      0,
      20
    ]
  ],
  "32s": [
    [
      0,
      20
    ]
  ],
  "33": [
    [
      0,
      20
    ]
  ],
pdata['AA'][0][1]
import * as pdata from './push.json';
import { Convert } from './Convert'
    
const pushHands = Convert.toHands(JSON.stringify(pdata));
var ch = '42o';
console.log(pushHands['default'][ch][0][1]);

我也没有让这个工作,但无论如何谢谢。我找到了使用转换器的解决方案。我也没有让这个工作,但无论如何谢谢。我找到了使用转换器的解决方案。