1

I really need a hand here. In the last 2 or 3 hours I tried to simply list a JSON object in freemarker with a javascript backed webscript and no luck.

let's name my script as x, so:

x.get.js

function main(){
    //model.data = {'name':'Test Object','size':100};
    model.data={
        "2012": {
            "A": {
                "a": "on",
                "b": "off",
                "list": [
                    1,
                    2,
                    3,
                    4
                ]
            }
        },
        "2013": {
            "B": {
                "c": "on",
                "d": "off"
            }
        }
    };

   logger.log(jsonUtils.toJSONString(model.data));
}

main();

x.get.html.ftl

<#assign keys = data?keys>
<#list keys as tag>
   ${jsonUtils.encodeJSONString(tag)}<#if tag_has_next>,</#if>
</#list>

When I try just with the first model.data (commented out) it works fine, and the logger writes out the full JSON, i get the "name, size" in the rendered HTML.

However, when I try like this, I get nothing in the rendered HTML, and logger says weird things:

 {"2012":"org.mozilla.javascript.UniqueTag@1a31e0a: NOT_FOUND","2013":"org.mozilla.javascript.UniqueTag@1a31e0a: NOT_FOUND"}

So what? This JSON is valid too, so I miss some conversation or misterious function or what?

Thanks for the help

J.

  • 1
    Okay, I worked it out! The logger also logs weird things for the following json: `model.data = {'123':'Test Object','size':100};` exactly: `{"123":"org.mozilla.javascript.UniqueTag@1a31e0a: NOT_FOUND", 'size' : 100}` When the controller script gets executed, if you have a decimal in a string (what you expect to be interpreted as a string, because this is a valid json!), like "123", it will be parsed as a decimal! I don't know why. I'm searching for answers. **So don't use integers in string literals, as JSON identifiers**, when playing with web scripts. – József Gubicza Jul 18 '12 at 20:32

1 Answers1

0

normally you should fill your model using nornmal objects/values in js and generate the json in your ftl template x.get.json.ftl

alfrescian
  • 4,079
  • 1
  • 18
  • 20