3

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)
...
jwismar
  • 12,164
  • 3
  • 32
  • 44
user1884155
  • 3,616
  • 4
  • 55
  • 108
  • 1
    How are you reading this? – Syam S Aug 08 '14 at 14:07
  • Your error is elsewhere. I have read a properties file with unescaped JSON as value with a `ResourceBundle` and no `NumberFormatException`. Which would have been crazy weird anyway. – Mena Aug 08 '14 at 14:09
  • When you read a Properties file there is no need to escape the contents. Your JSON array doesn't contain numbers. Post relevant code and the Exception. – Elliott Frisch Aug 08 '14 at 14:10
  • `''` would definitely be illegal json anyways. json quotes are `"`, and nothing else. – Marc B Aug 08 '14 at 14:10
  • @ElliottFrisch I added the relevant code and exception – user1884155 Aug 08 '14 at 14:36
  • @SyamS by using a struts tag in my jsp page – user1884155 Aug 08 '14 at 14:37
  • Clearly `"Name":"Afghanistan"` is not a number. My guess, is you are trying to parse everything to an int for the `TelephoneCode` and that fails on everything else. – Elliott Frisch Aug 08 '14 at 14:40
  • @ElliotFrisch I'm not trying to parse anything, this is all standard struts2/jsp code. The error occurs on page loading and I don't touch the resourcebundle anywhere else. If I replace the "json string" with some dummy value that doesn't use quotes or braces, the input on my jsp page correctly gets that value. Something must be wrong with the property loader/getter that it tries to convert the string to an int – user1884155 Aug 08 '14 at 14:43
  • 1
    `myjson='{"SOMETHING1" : "S", "SOMETHING2" : "T", "SOMETHING3" : "U"}'` is the format that worked for me, the quotes are used to escape – EpicPandaForce Apr 16 '15 at 08:03
  • @EpicPandaForce so you only need quotes once, at the beginning and the end, not at every single bracket charatcer? that's great :) – user1884155 Apr 16 '15 at 10:33
  • Yep! It's specified in http://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html and also the cause of problem for apostrophes as per http://stackoverflow.com/a/4449670/2413303 . It took a while to figure it out – EpicPandaForce Apr 16 '15 at 10:38

1 Answers1

4

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"'}',...]

Syam S
  • 8,421
  • 1
  • 26
  • 36
  • This was exactly the problem and the right solution, and it also explains why he was trying to parse the content between { } to a number! Thanks – user1884155 Aug 08 '14 at 14:52