Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 565
user2566484 Avatar asked Dec 13 '15 21:12

user2566484


People also ask

Can we convert string to JSON in Java?

There are the following three libraries are used to convert String to JSON Object in Java: Using Gson Library. Using JSON-Simple Library. Jackson Library.

What is JSON object in Android?

JSON (JavaScript Object Notation) is a straightforward data exchange format to interchange the server's data, and it is a better alternative for XML. This is because JSON is a lightweight and structured language.

How do I convert a JSON object to a string?

Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.

How check JSONObject is null or not in android?

Try with json. isNull( "field-name" ) .


1 Answers

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?

like image 131
j2emanue Avatar answered Sep 20 '22 02:09

j2emanue