代码之家  ›  专栏  ›  技术社区  ›  user1592380

无法使用导入的JSON将对象转换为基元值

  •  0
  • user1592380  · 技术社区  · 7 年前

    enter image description here

    我正在尝试在Nuxt项目中动态构建Vuetify组件( Using different text values with vuetify component )通过在模块中导入和迭代JSON( https://hackernoon.com/import-json-into-typescript-8d465beded79 )

    我在/static/info.json中的json是:

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

    在我的Vue组件中,我有:

      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'
    

    线:

      var jsonData = JSON.parse(data);
    

    原因:

     Cannot convert object to primitive value 
    

    如何迭代导入的JSON?

    1 回复  |  直到 7 年前
        1
  •  4
  •   Matthew Page    7 年前

    我猜数据已经是一个对象,不需要再次解析。导入已将其转换为对象。您已经将它与data.name一起使用了。