user1884155
user1884155

Reputation: 3736

Escaping a json string inside a properties file

I use typical properties file in a java environment. I want one of the properties to be a json array:

#Country list in json by language
countries = [{"Name":"America"},{"Name":"Germany"},...]

This gives a number format exception in java when trying to read the string mapped to countries.

I tried a bunch of escaping sequences, but none seem to work:

countries = [{\"Name\":\"America\"},{\"Name\":\"Germany\"},...]
countries = [{\\"Name\\":\\"America\\"},{\\"Name\\":\\"Germany\\"},...]
countries = [{''Name'':''America''},{''Name'':''Germany''},...]

I'm wondering why a number formatexception is thrown, considering this is a string? Also, what is wrong with a json string that makes the file flip? Is it the [, {, " or : character(s)?

EDIT:

Her's the actual json in my properties file:

countries_json = [{"Name":"Afghanistan","Code":"AF","TelephoneCode":"+93"},{"Name":"Belgium","Code":"BE","TelephoneCode":"+32"}]

Here's the code in my jsp page which gets this value:

<input type="hidden" id="countryListJSON" value='<s:text name="countries_json"/>'/>

And here's the exception that happens on the java backend when the jsp is being rendered:

SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NumberFormatException: For input string: ""Name":"Afghanistan""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.text.MessageFormat.makeFormat(Unknown Source)
    at java.text.MessageFormat.applyPattern(Unknown Source)
    at java.text.MessageFormat.<init>(Unknown Source)
    at com.opensymphony.xwork2.util.LocalizedTextUtil.buildMessageFormat(LocalizedTextUtil.java:704)
    at com.opensymphony.xwork2.util.LocalizedTextUtil.getDefaultMessage(LocalizedTextUtil.java:663)
    at com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:534)
    at com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:259)
    at com.opensymphony.xwork2.ActionSupport.getText(ActionSupport.java:131)
    at org.apache.struts2.util.TextProviderHelper.getText(TextProviderHelper.java:75)
    at org.apache.struts2.components.Text.end(Text.java:160)
    at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
    at org.apache.jsp.shared.jsp.RegisterForm_jsp._jspx_meth_s_005ftext_005f14(RegisterForm_jsp.java:874)
    at org.apache.jsp.shared.jsp.RegisterForm_jsp._jspService(RegisterForm_jsp.java:177)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
...

Upvotes: 3

Views: 11924

Answers (1)

Syam S
Syam S

Reputation: 8499

The struts tag might be treating it as a message argument. So its expecting a {0}, {1} etc. Give escape quotes for { in your property file like countries = ['{'"Name":"America"'}','{'"Name":"Germany"'}',...]

Upvotes: 5

Related Questions