简体   繁体   中英

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

the value of 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)

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,

note the label can be change it depends on the string that saves on draft column

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:

'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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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