简体   繁体   中英

Cannot convert object to primitive value with imported json

在此处输入图片说明

I'm trying to dynamically build a vuetify component in a nuxt project ( Using different text values with vuetify component ) by importing and iterating through json in a module (https://hackernoon.com/import-json-into-typescript-8d465beded79 ).

My json in /static/info.json is:

{
  "id": 1,
  "name": "Johnson, Smith, and Jones Co.",
  "amount": 345.33,
  "Remark": "Pays on time"
}

In my vue component I have:

  import * as data from '../static/info.json';

  const word = data.name;

  console.log(word); // output 'testing'
  console.log(data); // output 'testing'
  var jsonData = JSON.parse(data);
  // console.log(jsonData); // output 'testing'

The line:

  var jsonData = JSON.parse(data);

causes:

 Cannot convert object to primitive value 

How can I iterate through the imported json?

I would guess data is already an object and doesn't need to be parsed again. The import has turned it into an object. You've already used it with data.name

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