0

I need to produce json Output with numbers and string, both in double quotes in my REST API call.

Example:

[
    {
       "LastName":"abc", 
       "FirstName":"xyz", 
       "Id":"123", 
     },
    { 
      "LastName":"mno", 
      "FirstName":"pqr", 
      "Id":"456", 
    }
 ]

As you can see the above output is what I require.

However the numbers 123 and 456 appear without double quotes as they are numbers. Does anyone know a workaround?

I am using org.json.JSONObject. My application first produces an XML which is then converted to JSON using the following lines of code:

JSONObject json = XML.toJSONObject(xml);
String jsonString = json.toString(PRINT_INDENT_FACTOR);

The jsonString above is directly rendered on browser in the REST API call. The numbers appear without double quotes. Is there any property under the JSONObject library which can solve this issue?

Thanks in advance!

====================== EDIT 1 ===================

I tried the following code based on the suggestions and the link provide in the comments by changing the library to use GSon 2.2.2 jar instead of JSONObject

GsonBuilder gb = new GsonBuilder();
gb.setPrettyPrinting();
gb.disableHtmlEscaping();
gb.setLongSerializationPolicy( LongSerializationPolicy.STRING );
Gson gson = gb.create();
File fXmlFile = new File("\\path\\test.xml");
String str =  FileUtils.readFileToString(fXmlFile);
System.out.println(gson.toJson(str));

However the output still is with numbers without double quotes!!!! :(

what am i doing wrong?

====================== EDIT 2 ===================

Ok, LongSerializationPolicy.STRING works only for long type and not decimal\float. So the long values show up in double quotes. Then what about int , float and decimals. Those are numerical values too. How can I show them in double quotes?

====================== EDIT 3 ===================

Changed my libraries to jackson one and used the jackson-dataformat-xml-2.9.2.jar to get the JSON. So the numbers appear in double quotes now. Decimals, floats, ints etc. However the entire XML is not parsed. Only a section of the tags is parsed. its missing many other tags. I have read another post here that it is known to have limitations.

====================== EDIT 4 ===================

I have just done a string replace all using regex. That works.

jsonString = jsonString.replaceAll(":\s([\d]+)", ": \"$1\"");

Aman Mohammed
  • 2,878
  • 5
  • 25
  • 39
  • Possible duplicate of [Convert all the integer value to string in JSON](https://stackoverflow.com/questions/7389598/convert-all-the-integer-value-to-string-in-json) – Monkey D. Luffy Oct 23 '17 at 13:56
  • Check out [this](https://stackoverflow.com/a/23483692/3094731) answer. – Abdullah Khan Oct 23 '17 at 13:57
  • @AbdullahKhan , i would need to switch to a new library in that case. Isnt there something in the JSONObject library to allow me to set numbers with double quote – Aman Mohammed Oct 24 '17 at 10:05
  • @MonkeyD.Luffy , i dont think I can do that. The output is on the browser when we invoke the REST API call. That link is all about javascript client side. I am looking for a server side solution. – Aman Mohammed Oct 24 '17 at 10:08
  • That's not valid JSON to start with - you need `:` instead of `=`. – Jon Skeet Oct 24 '17 at 10:52
  • @JonSkeet , i have edited my post to correct it. – Aman Mohammed Oct 24 '17 at 12:57
  • If you use Long numbers in JSON, here is a [perfect explanation for double quotes](https://stackoverflow.com/a/34785421/1472483) – MarsPeople Oct 25 '19 at 08:28

2 Answers2

1

Just convert them to Strings before via Integer.toString(i) or String.valueOf(i)

EDIT:

If you are fancy you could just subclass JSONObject and overwrite the numberToString() method to inlcude the quotes

OR

Use some regex operation on the resulting string that adds the quotes. Unfortunately I am not good with regex but maybe someone else can help you

ChristophE
  • 760
  • 1
  • 9
  • 21
  • the data is in xml format which is converted to json using the JSONObject library. I dont have any java code were i can apply the above methods. If i have to do that, i will need to traverse through the json tree, find all numbers and enclose in double quotes. – Aman Mohammed Oct 24 '17 at 10:07
0

Done a string replace using regex as i couldnt find any other way around it.

jsonString = jsonString.replaceAll(":\s([\d]+)", ": \"$1\"");

Aman Mohammed
  • 2,878
  • 5
  • 25
  • 39