Home > Java > javaTutorial > 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
Release: 2023-08-21 08:49:15
forward
936 people have browsed it

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
   }
}
Copy after login

Output

Adithya
22
10000.0
false
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template