简体   繁体   English

字符串转换为json,如何获取label和数据?

[英]Converted string to json, how to get the label and data?

the value of props.record.DataUpdate.draft props.record.DataUpdate.draft 的值

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

let newData = JSON.parse(props.record.DataUpdate.draft)让 newData = JSON.parse(props.record.DataUpdate.draft)

console.log(newData)控制台日志(新数据)

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

this is my database looks like这是我的数据库

  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,
  },

what I want here is to get the label (Fistname,Lastname,Middlename) with there perspective value,我在这里想要的是获得具有透视值的 label (Fistname,Lastname,Middlename),

note the label can be change it depends on the string that saves on draft column请注意label 可以根据保存在草稿列中的字符串进行更改

how to achieve that?如何做到这一点?

Something like that:像这样的东西:

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]);
}

This gives me the Output:这给了我 Output:

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

Edit: Ok i took the example form your post (without the single quotes).编辑:好的,我以你的帖子为例(没有单引号)。 Then just do what you posted and then use the for loop:然后只需执行您发布的内容,然后使用 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]);
}

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

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