How to Convert a JSON Number to an Integer

JavaScript Object Notation (JSON) is a mark-up language designed to simplify data exchanges between Web browsers and servers. Unlike other mark-up languages, programmers do not have to define the type of variable before providing a value for the variable. For example, a coder would not encounter an error for failing to specify that a value is a whole number versus a number with a decimal. Sometimes a programmer may need to display or convert a JSON number from a float (a number with a decimal) to an integer (a whole number). Learning how to convert a JSON number to an integer allows you to use class-specific programming languages to modify data retrieved through JSON.

Instructions

    • 1

      Define the number in JSON. Follow the JSON format to create an object. Type "{ "number" : 1.5}" without quotations. Replace "number" and "1.5" with the name of your object and the float number you wish to convert.

    • 2

      Type "public int getInt(java.lang.number)" in your code editor without the quotation marks. Replace "number" with the name of your object from above.

    • 3

      Run the code to test for errors. Check that the object name matches if you receive a "JSONException." Remove any quotation marks from directly around the number (such as "1.5"). Quotation marks define the value as a character that cannot be converted to an integer.

Learnify Hub © www.0685.com All Rights Reserved