JSONObject是名称/值对的无序集合,它从字符串中解析文本以生成类似映射的对象。JSONObject有几个重要的方法来显示不同类型的值,比如getString()方法获取与键字符串相关联的字符串,getInt()方法获取与键关联的int值,getDouble()方法获取与键关联的double值,getBoolean()方法获取与键关联的布尔值。
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