渲染为JSON在Grails中不起作用

时间:2016-01-18 23:23:16

标签: grails serialization

我是Groovy和Grails的新手,我正在尝试创建一个返回JSON格式字符串的方法。

我读了一些关于转换器的内容,以及我从几个来源(herehere可以理解的内容)我应该能够做到这一点:

import grails.converters.JSON

class Record {

    //...

    private Map _metadata   = [:]

    String getMetadataJSON(){
        return render _metadata as JSON
    }
}

不仅这不起作用,而且#34;渲染"关键字尚未解析。

所以我的两个问题是:

  • 为什么这不起作用,应该怎么做?
  • 什么样的语言构造"渲染"应该是?,一个封闭?

1 个答案:

答案 0 :(得分:3)

## ## Auto Generated makefile by CodeLite IDE ## any manual changes will be erased ## ## Debug ProjectName :=AreaComp ConfigurationName :=Debug WorkspacePath := "/Users/tuckerfowler/Documents/CS3304" ProjectPath := "/Users/tuckerfowler/Documents/CS3304 /AreaComp" IntermediateDirectory :=./Debug OutDir := $(IntermediateDirectory) CurrentFileName := CurrentFilePath := CurrentFileFullPath := User :=Tucker Fowler Date :=18/01/2016 CodeLitePath :="/Users/tuckerfowler/Library/Application Support/codelite" LinkerName :=/usr/bin/g++ SharedObjectLinkerName :=/usr/bin/g++ -dynamiclib -fPIC ObjectSuffix :=.o DependSuffix :=.o.d PreprocessSuffix :=.i DebugSwitch :=-g IncludeSwitch :=-I LibrarySwitch :=-l OutputSwitch :=-o LibraryPathSwitch :=-L PreprocessorSwitch :=-D SourceSwitch :=-c OutputFile :=$(IntermediateDirectory)/$(ProjectName) Preprocessors := ObjectSwitch :=-o ArchiveOutputSwitch := PreprocessOnlySwitch :=-E ObjectsFileList :="AreaComp.txt" PCHCompileFlags := MakeDirCommand :=mkdir -p LinkOptions := IncludePath := $(IncludeSwitch). $(IncludeSwitch). IncludePCH := RcIncludePath := Libs := ArLibs := LibPath := $(LibraryPathSwitch). ## ## Common variables ## AR, CXX, CC, AS, CXXFLAGS and CFLAGS can be overriden using an environment variables ## AR := /usr/bin/ar rcu CXX := /usr/bin/g++ CC := /usr/bin/gcc CXXFLAGS := -g -O0 -Wall $(Preprocessors) CFLAGS := -g -O0 -Wall $(Preprocessors) ASFLAGS := AS := /usr/bin/as ## ## User defined environment variables ## CodeLiteDir:=/Users/tuckerfowler/Downloads/codelite.app/Contents/SharedSupport/ Objects0=$(IntermediateDirectory)/main.cpp$(ObjectSuffix) Objects=$(Objects0) ## ## Main Build Targets ## .PHONY: all clean PreBuild PrePreBuild PostBuild MakeIntermediateDirs all: $(OutputFile) $(OutputFile): $(IntermediateDirectory)/.d $(Objects) @$(MakeDirCommand) $(@D)@echo "" > $(IntermediateDirectory)/.d @echo $(Objects0) > $(ObjectsFileList) $(LinkerName) $(OutputSwitch) $(OutputFile) @$(ObjectsFileList) $(LibPath) $(Libs) $(LinkOptions) MakeIntermediateDirs: @test -d ./Debug || $(MakeDirCommand) ./Debug $(IntermediateDirectory)/.d: @test -d ./Debug || $(MakeDirCommand) ./Debug PreBuild: ## ## Objects ## $(IntermediateDirectory)/main.cpp $(ObjectSuffix):main.cpp $(IntermediateDirectory)/main.cpp $(DependSuffix) $(CXX) $(IncludePCH) $(SourceSwitch) "/Users/tuckerfowler/Documents/CS3304/AreaComp/main.cpp" $(CXXFLAGS) $(ObjectSwitch) $(IntermediateDirectory)/main.cpp $(ObjectSuffix) $(IncludePath) $(IntermediateDirectory)/main.cpp $(DependSuffix): main.cpp @$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath)-MG-MP-MT $(IntermediateDirectory)/main.cpp $(ObjectSuffix) -MF $(IntermediateDirectory)/main.cpp $(DependSuffix) -MM "main.cpp" $(IntermediateDirectory)/main.cpp $(PreprocessSuffix): main.cpp @$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/main.cpp $(PreprocessSuffix) "main.cpp" -include $(IntermediateDirectory)/*$(DependSuffix) ## ## Clean ## clean: $(RM) -r ./Debug/ 用于Grails中的Controller,而不是(看起来像是)Domain Class(在您的示例中)。如果你想获得某些东西的JSON表示,那么只需:

render

以上内容将以String getMetadataJSON() { (_metadata as JSON) } 格式返回String个代表。 Groovy不需要JSON关键字。