Android Convert String for JSONObject

I want to convert a string to a JSONObject. The following is sample code.

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

But after the conversion time becomes equal to 1,4498385980999203E9. Any help would be greatly appreciated. Thanks

+5
source share
3 answers

If you really want it to be formatted this way, make it into a string instead of a number. Just add double quotes around the element. But if you use it to calculate on the other end, I would leave it as it is, since you still have to convert it back to int. If you want to use gson as an alternative here: How to prevent the conversion of a large number of Gson (json strings) to scientific notation format?

+3
source

Write it as shown in double quotation marks. "1449838598,0999202"

+2
source

As Abhishek said, write the time and price in double quotation marks as "1449838598.0999202" and "52". Then you can save the string in 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); 
+1
source

Source: https://habr.com/ru/post/1238037/


All Articles