简体   繁体   English

如何将 JSON 属性解析为 Typescript 中的 Map 键?

[英]How to parse JSON property as Map key in Typescript?

I want to parse JSON property as a Map key.我想将 JSON 属性解析为 Map 键。 Here is JSON string:这是 JSON 字符串:

const jsonString = `{
    "DDBProps": {
      "us-east-1": {
        "test": "2",
        "prod": "100"
      },
      "us-west-2": {
        "test": "2",
        "prod": "100"
      }
    },
    "DDBProps2": {
      "us-east-1": {
        "test": "2",
        "prod": "200"
      },
      "us-west-2": {
        "test": "2",
        "prod": "200"
      }
    }
}`

So, I want these "DDBProps", "DDBProps2", ... "DDBPropsX" to be a Map key, and value to be nested Map. Essentially, something like Map<string, Map<string, Map<string, string>>> .所以,我希望这些“DDBProps”、“DDBProps2”、...“DDBPropsX”是一个 Map 键,嵌套值 Map。本质上,类似于Map<string, Map<string, Map<string, string>>> I want this nest map structure because I need to get the number (eg "2", "100", "200" in this JSON) based on the input: DDBPropsType, region, stage .我想要这个 nest map 结构,因为我需要根据输入DDBPropsType, region, stage获取数字(例如这个 JSON 中的“2”、“100”、“200”)。

So I declared interfaces:所以我声明了接口:

interface ThroughputMap {
    throughputMappings: Map<string,RegionMap>
}

interface RegionMap {
    regionMappings: Map<string, StageMap>
}

interface StageMap {
    stageMappings: Map<string, string>
}

// parse
const ddbScaleMap: ThroughputMap = JSON.parse(ddbScalingMappingsJson);

However, this doesn't parse the JSON to correct structure:但是,这不会解析 JSON 以更正结构:

console.log(ddbScaleMap)
// output below
{
    "DDBProps": {
      "us-east-1": {"test": "2","prod": "100"},
      "us-west-2": {"test": "2","prod": "100"}
    },
    "DDBProps2": {
      "us-east-1": {"test": "2","prod": "200"},
      "us-west-2": {"test": "2","prod": "200"}
    }
}


console.log(ddbScaleMap.throughputMappings) // output undefined

I can't parse property as Map key.我无法将属性解析为 Map 键。

I tried:我试过了:

const ddbScaleMap = new Map<string, Map<string, Map<string, string>>>(Object.fromEntries(JSON.parse(jsonString)))

This didn't parse correctly in nested value.这在嵌套值中没有正确解析。 Meaning, ddbScaleMap.get("DDBProps").get("us-west-2");意思是, ddbScaleMap.get("DDBProps").get("us-west-2"); .get("us-west-2") cannot be called.无法调用 .get("us-west-2")。

So apparently, there is a way to access nested value easily without converting to a map. In TS, you use keyof typeof to access nested value like this:显然,有一种方法可以轻松访问嵌套值而无需转换为 map。在 TS 中,您可以使用keyof typeof来访问嵌套值,如下所示:

function getThroughputByKey(key: string, region: string, stage: string): number {
    const defaultThroughputValue = 1;

    const regionToStageMap = ddbScalingMappingsObject[key as keyof typeof ddbScalingMappingsObject];
    const stageToThroughputMap = regionToStageMap[region as keyof typeof regionToStageMap];
    const throughputString = stageToThroughputMap[stage as keyof typeof stageToThroughputMap];
    const throughput = Number(throughputString);

    return throughput || defaultThroughputValue;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM