开发者

Freemarker, Maps and Mongo. Getting a value out a nested structure

开发者 https://www.devze.com 2023-03-21 19:07 出处:网络
I have a Mongo Database and I am making a small web application to display the values from the database. I\'m having a little problem with getting a value from a nested structure.

I have a Mongo Database and I am making a small web application to display the values from the database. I'm having a little problem with getting a value from a nested structure.

The database structure I have is:

{ 

    "_id": ObjectId("4e244ddcb1633b25c38f2155"),
    "id": "2173",
    "type": "P",
    "title": "Test title", 
    "tag": {
      "0": {
      "_id": ObjectId("4e244ddcb1633b25c38f1fc1"),
      "name": "Education",
      "id": "6"
 }
}  
}

What I do is, I get the database from the database and put this in a map and this map I put together with several other maps into one map (root) and put that into the .ftl page. I can then read the value in .ftl like: ${root.title} which will ofcourse 开发者_如何学Goprint 'Test title' in the above example.

I now what to get the value of name in tage so that it can print 'Education'

I don't know how to get this. Any ideas.


You can access the value using Freemarker's built-in's for hashes. Similar to below:

<#assign tagKeys = root.tag?keys>
<#list tagKeys as tagKey>
    ${root.tag[tagKey].name}
</#list>

I have not tested this yet but generally speaking this should work. You might also want to read some previous answers on SO:

Freemarker iterating over hashmap keys

0

精彩评论

暂无评论...
验证码 换一张
取 消