Asad
Asad

Reputation: 357

Android Convert String to JSONObject

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

Upvotes: 6

Views: 1933

Answers (3)

AmitSandesara
AmitSandesara

Reputation: 49

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

Upvotes: 1

Abhishek
Abhishek

Reputation: 1674

Write it like this in double quote. "1449838598.0999202"

Upvotes: 2

j2emanue
j2emanue

Reputation: 62519

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?

Upvotes: 4

Related Questions