cancel
Showing results for 
Search instead for 
Did you mean: 

jsonUtils.encodeJSONString(x) json

xsp
Champ in-the-making
Champ in-the-making
Hi,

I have a webscript that accpet the get as html and json. The html runs fine, but the json ones return a …Expression jsonUtils.encodeJSONString(x) … error  The exact error is:


"message" : "10080009 Wrapped Exception (with status template): 10080137 Error during processing of the template 'Expression jsonUtils.encodeJSONString(x) is undefined on line 1, column 15 in com\/test\/countries.get.json.ftl.'. Please contact your system administrator.", 
  "exception" : "org.springframework.extensions.webscripts.WebScriptException - 10080009 Wrapped Exception (with status template): 10080137 Error during processing of the template 'Expression jsonUtils.encodeJSONString(x) is undefined on line 1, column 15 in com\/test\/countries.get.json.ftl.'. Please contact your system administrator.",

My json script (countries.get.json.ftl, …which returns the countries which are a custom type) is:


<#escape x as jsonUtils.encodeJSONString(x)>
{
"Results" : [
  <#list countries as country>
    {"label":"${country.label}", "value":"${country.value}"},
   </#list>
   ]
}
</#escape>

Can anyone tell what is wrong in the json script? …also, I didn't find proper documentation of how to writting a simple json script for a custom type.

Thanks in advance.
2 REPLIES 2

abarisone
Star Contributor
Star Contributor
Hi,
first of all if you need to escape your results use Freemarker's built-ins for Strings (and many other objects) as stated here http://freemarker.sourceforge.net/docs/index.html
Your ftl should look like
{
"Results" : [
  <#list countries as country>
    {"label":"${country.label?js_string}", "value":"${country.value?js_string}"},
   </#list>
   ]
}
where js_string converts your results in Javascript compliant strings.
Then the it's up to you to build up a correct json structure.
Remember that here you are creating the View part of a MVC pattern.
You can find more info about JSON here http://www.json.org/

Regards,
Andrea

jhesparrach
Champ in-the-making
Champ in-the-making
Hi,
you get this error when the json is not well built. In this case, your problem is the comma.

Your code should be like that:

<#escape x as jsonUtils.encodeJSONString(x)>
{
"Results" : [
  <#list countries as country>
    {"label":"${country.label}", "value":"${country.value}"}<#if countries_has_next>,</#if>
   </#list>
   ]
}
</#escape>

Regards