6

I want to convert a string to JSONObject. Below is sample code.

String str = "{"time": 1449838598.0999202, "Label": "Shirt", "Price": 52}";
JSONObject obj = new JSONObject(str);

But after conversion time becomes 1.4498385980999203E9. Any Help will be appreciated. Thanks

Asad
  • 357
  • 1
  • 7
  • 15

3 Answers3

4

If you really want it to be formatted like that then make it into a string instead of a number. Just add double quotes around the item. But if your using it for a calculation on the other end, i would leave it as is as you'll have to convert it back to a int anyway. If you want to use gson as an alternative check here: How to prevent Gson from converting a long number (a json string ) to scientific notation format?

Community
  • 1
  • 1
j2emanue
  • 60,549
  • 65
  • 286
  • 456
2

Write it like this in double quote. "1449838598.0999202"

Abhishek
  • 1,654
  • 2
  • 18
  • 31
1

Like Abhishek said, write time & price in double quote as "1449838598.0999202" and "52". Then you can save the string to Gson, and then convert it to Json.

String str = "{"time": "1449838598.0999202", "Label": "Shirt", "Price": "52"}";
Gson gson = new Gson();
String json = gson.toJson(str);
AmitSandesara
  • 35
  • 1
  • 7