2013-08-23 20 views
3

獲取鍵和值我有一個JSON文件是這樣的...速度:從JSON

{ 
"template" : "test-template", 
"url" : "url", 
"imageCount" : 4, 
"pageSize" : 36, 
"class" : "test-class" 
} 

現在我知道我可以訪問各個參數。例如,訪問$template將打印test-template

有沒有辦法遍歷整個集合並獲得鍵和值?

+0

也許你可以使用ContextTool這個?如果沒有,請在上下文中添加相同效果的工具。 https://velocity.apache.org/tools/devel/summary.html – wau

回答

1
#foreach($key in $json.keySet()) 
    <div>$key : $json.get($key)</div> 
#end 
+1

請不要代碼只回答 – danopz

2
  1. 您可以創建通過擴展類SecureUberspector和覆蓋方法getIterator爲JSONObject的(使用運營商的情況下)的自定義uberspector。

  2. 通過實現接口迭代器來創建迭代器類。使用一些Json植入具有返回Iterator的keys()方法,其他具有entrySet()方法返回Set <條目< String,JsonObject >>的事實。

參考鏈接:http://ajdevtech.blogspot.in/2016/02/blog-post.html