- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the values of the different types from a JSON object in Java?
A JSONObject is an unordered collection of name/value pairs and parses text from a String to produce a map-like object. A JSONObject has few important methods to display the values of different types like getString() method to get the string associated with a key string, getInt() method to get the int value associated with a key, getDouble() method to get the double value associated with a key and getBoolean() method to get the Boolean value associated with a key.
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
Advertisements