如何在Java中从JSON对象中获取不同类型的值?

来自:互联网
时间:2023-08-21
阅读:

示例

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

 

以上就是如何在Java中从JSON对象中获取不同类型的值。

返回顶部
顶部