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

4
  • Write it like this"1449838598.0999202" in double quotes Commented Dec 13, 2015 at 21:14
  • What command are you using to retrieve the "time" attribute? Commented Dec 13, 2015 at 21:22
  • 1
    I dont see anything wrong with scientific notation Commented Dec 13, 2015 at 21:28
  • Try adding the escape character in your string initialization: String str = "{\"time\": 1449838598.0999202, \"Label\": \"Shirt\", \"Price\": 52}"; Commented Dec 13, 2015 at 21:47

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

Comments

2

Write it like this in double quote. "1449838598.0999202"

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.