Home  >  Article  >  Java  >  How to get different types of values ​​from JSON object in Java?

How to get different types of values ​​from JSON object in Java?

WBOY
WBOYforward
2023-08-21 08:49:15881browse

How to get different types of values ​​from JSON object in Java?

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.

Example

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
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete