Skip to content

Instantly share code, notes, and snippets.

@czy88840616
Last active December 11, 2015 20:18
Show Gist options
  • Save czy88840616/4654130 to your computer and use it in GitHub Desktop.
Save czy88840616/4654130 to your computer and use it in GitHub Desktop.
一些vm变成json的例子

##简单变量##

{
#if($errorMsg)
    status:false,
	errorMsg:$errorMsg
#else
    status:true,
	userNick:$!userNick
#end
}

===>

{
    "errorMsg":null, //可以没有
    "userNick":"tbtest001"
}

##map## entrySet、key、value、size都是java的Map类自带的属性或者方法

{
"shareListInfo":
[
  #foreach($member in $unReadCountMap.entrySet())
  {
	"shareListId":$member.key,
	"unreadCount":$member.value
  },
  #end 
]
}

===>

{
  "unReadCountMap":{
    "1":20,
    "2":"99+",
    "3":"0"
  }
}

##数组Array##

#foreach($serviceInfo in $serviceInfos)
    "$serviceInfo.getIdentifier()": {
        "title": "$serviceInfo.title",
        "agio": "$serviceInfo.description"
    }
    #if("$velocityCount" != $serviceInfos.size())
        ,
    #end
#end

===>

velocityCount为velocity内置变量,getIdentifier方法可以通过属性模拟,size也是数组的内置方法,无需模拟

{
  "serviceInfos":[
	  {
	    "getIdentifier":"1",
	    "title":"数据1",
	    "description":"描述"
	  },
	  {
	    "getIdentifier":"2",
	    "title":"数据2",
	    "description":"描述"
	  },
	  {
	    "getIdentifier":"3",
	    "title":"数据3",
	    "description":"描述"
	  }
  ]
}

##复杂函数模拟## 比如,带参数,或者逻辑里需要根据参数进行动态运算的,例如 这是一段根据传入的参数返回不同展现的代码

$shareUtil.getMsgCountStr($userShare.UnreadMsg)

===>

通过定义一个函数式的字符串来解决这个问题,函数式的语法也是javascript

{
	"shareUtil":{
		"getMsgCountStr":"function(v){return v>99?'99+':v}"
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment