简体   繁体   中英

Javascript JSON string to object

When I query a database, I receive a string as follows:

{
  S: '[{"firstName":"Max","lastName":"Mustermann","age":40},{"firstName":"Hagbard","lastName":"Celine","age":44},{"firstName":"Karl","lastName":"Koch","age":42}]'
}

How can I go about converting the string above to JSON objects to receive something like this:

var someData = [
  {firstName: "Max", lastName: "Mustermann", age: 40},
  {firstName: "Hagbard", lastName: "Celine", age: 44},
  {firstName: "Karl", lastName: "Koch", age: 42},
];

Would appreciate any assistance at all, thank you very much!

 const receivedFromDB = { S: '[{"firstName":"Max","lastName":"Mustermann","age":40},{"firstName":"Hagbard","lastName":"Celine","age":44},{"firstName":"Karl","lastName":"Koch","age":42}]' }; const arrayOfData = JSON.parse(receivedFromDB.S); console.log(arrayOfData);

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