NodeJS 把字符串转换成json,如何得到标签和数据?

z4bn682m  于 2022-12-18  发布在  Node.js
关注(0)|答案(1)|浏览(118)

属性.记录.数据更新.草稿的值

{"FirstName": "This is my firstname","MiddleName": "This is my middlename","LastName": "this is my lastname"}

让新数据= JSON解析(属性记录数据更新草稿)
控制台日志(新数据)

{
  "FirstName": "This is my firstname",
  "MiddleName": "This is my middlename",
  "LastName": "this is my lastname"
}

这是我的数据库

id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true,
  },
  tableNameToBeUpdated: {
    type: Sequelize.STRING,
  },
  tableIdToBeUpdated: {
    type: Sequelize.STRING,
  },
  draft: {
    type: Sequelize.JSON,
  },
  CreatedDate: {
    type: Sequelize.DATE,
  },
  LastUpdateDate: {
    type: Sequelize.DATE,
  },
  status: {
    type: Sequelize.STRING,
  },


这里我想要的是得到标签(名字,姓氏,中间名)和透视值,

  • 注 * 标签可以更改,这取决于保存在草稿列上的字符串

如何做到这一点?

qltillow

qltillow1#

大概是这样:

const jsonString = {"FirstName": "This is my firstname","MiddleName": "This is my middlename","LastName": "this is my lastname"}

for (var key in jsonString) {
       console.log(key + " -> " + jsonString[key]);
}

这将给出输出:

'FirstName -> This is my firstname'
'MiddleName -> This is my middlename'
'LastName -> this is my lastname'

编辑:好的,我从你的帖子中取了这个例子(没有单引号)。然后按照你的帖子做,然后使用for循环:

const jsonString = '{"FirstName": "This is my firstname","MiddleName": "This is my middlename","LastName": "this is my lastname"}'

const jsonObject = JSON.parse(jsonString)

for (var key in jsonObject) {
  console.log(key + " -> " + jsonObject[key]);
}

相关问题