jsonangularng2-smart-table

how to parse a json text on angular array of object .? and display it on ng 2 smart table


I'm doing the maintenance of an angular website, I have a problem of display in the ng2 smart table , in a column it displays the json of an object, this is the error that the developer has made by receiving the list of transaction where the user and items are a json text, enter image description here how can I parse them

ps this is the code :

this.data.doData('transaction','get').subscribe({
      next:res=>{
        console.log(res);
       this.dataSource = [...res['transactions']]
        console.log(this.dataSource)        
      },error:err=>{
        console.log(err);
        
      }
    })


Solution

  • So actually you are getting the JSON string. To use it just do JSON.parse(items) and JSON.parse(user) and you will get the parsed JSON (array for the items and pbject for the user)

    You can also use map in your service file and handle this parsing their so when you subscribe to the data in your component's ts file you will get the parsed form.

    UPDATE:

    Look at the quotes (quotes matching you would know from basic rules of strings, i.e. either single or double should be enclosing the main string). So when you were JSON parsing the string it wasn't taking it correct format to parse it.

    enter image description here