【问题标题】:convert TypeScript object to JSON将 TypeScript 对象转换为 JSON
【发布时间】:2022-01-03 04:27:42
【问题描述】:

我想将 typecipt 对象转换为 JSON

ObjectToConvert = { key : value }

public getJson(key: string, value: string) {
    //I passed this values when I called the method : key="attr",value="on"
   console.log(JSON.stringify({ ObjectToConvert }))
   // The result is {"key":"on"} instead of {"attr":"on"}
   

我想知道为什么结果不是我作为参数传递的。

【问题讨论】:

    标签: javascript json typescript object


    【解决方案1】:

    使用computed property names:

    ObjectToConvert = { [key] : value }
    

    【讨论】:

    • 成功了,谢谢你的帮助:)
    猜你喜欢
    • 1970-01-01
    • 2019-11-07
    • 2018-03-26
    • 2014-05-17
    • 2016-05-23
    相关资源
    最近更新 更多