【问题标题】:Create Nodejs JSON Object [closed]创建 Nodejs JSON 对象 [关闭]
【发布时间】:2021-11-27 16:06:12
【问题描述】:

我想在 Nodejs 中创建一个 json 对象。我对 Nodejs 和 Javascript 还很陌生。下面是json格式。

{
  "Req": {
  "addr": ["teleNumber:021544"],
  "sendAddre": "teleNumber:347889",
  "outSMSBinaryMessage": {"message": "asdfa12547"},
  "validityPeriod": "10"
  }
}

【问题讨论】:

  • 那么您想从 Json 字符串创建一个 Javascript 对象吗? :困惑:

标签: javascript node.js json typescript


【解决方案1】:

JSON 是 JavaScript 对象表示法。您添加的是一个 JS 对象。如果要创建 JSON 字符串,请使用 JSON.stringify(yourObject)。如果我们想解析那个 JSON 字符串,请使用 JSON.parse(yourObjectStringified)

【讨论】:

    【解决方案2】:

    您是否希望创建接口或类?首先,您可以使用在线工具将 JSON 转换为 Typescript 接口。

    https://jsonformatter.org/json-to-typescript

    export interface Welcome10 {
    Req: Req;
    }
    
    export interface Req {
        addr:                string[];
        sendAddre:           string;
        outSMSBinaryMessage: OutSMSBinaryMessage;
        validityPeriod:      string;
    }
    
    export interface OutSMSBinaryMessage {
        message: string;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 2015-05-23
      • 2016-10-16
      • 2015-04-15
      相关资源
      最近更新 更多