A JSONObject is an unordered collection of key-value pairs and parses a text string to generate something like map object. A JSONObject has several important methods to display different types of values, such as the getString() method is used to get the string associated with the key string, getInt() method is used to get the integer value associated with the key, getDouble() method is used to get the double value associated with the key, getBoolean() method is used to get the double value associated with the key The associated boolean value.
import org.json.*; public class JSONObjectTypeValuesTest { public static void main(String[] args) throws JSONException { JSONObject jsonObj = new JSONObject( "{" + "Name : Adithya," + "Age : 22, " + "Salary: 10000.00, " + "IsSelfEmployee: false " + "}" ); System.out.println(jsonObj.getString("Name")); // returns string System.out.println(jsonObj.getInt("Age")); // returns int System.out.println(jsonObj.getDouble("Salary")); // returns double System.out.println(jsonObj.getBoolean("IsSelfEmployee")); // returns true/false } }
Adithya 22 10000.0 false
The above is the detailed content of How to get different types of values from JSON object in Java?. For more information, please follow other related articles on the PHP Chinese website!