2013-07-11 40 views

回答

2

我把json文件放入grails-app/conf/resources並編輯爲Bootstrap.groovy

在文件的頂部,我注入Grails應用程序:

class BootStrap { 
    def grailsApplication 
    ... 

,後來我加載的數據:

if (!Cars.count()) { 
     // add cars 
     def filePath = "resources/carsData.json" 
     def text = grailsApplication.getParentContext().getResource("classpath:$filePath").getInputStream().getText() 
     def json = JSON.parse(text) 

     for (carData in json) { 
      def c = new Cars(
       name: carData ["name"], 
       year: carData ["year"] 
      ).save(failOnError: true); 
     } 
    } 
+0

我一直在試圖通過解組而不是要做到這一點這種手動方式。任何想法如何做到這一點? –

相關問題