Table of Contents
Example
Output
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?

Aug 21, 2023 am 08:49 AM
json parsing json library in java Get the value in json object

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use Gin framework to implement XML and JSON data parsing functions Use Gin framework to implement XML and JSON data parsing functions Jun 22, 2023 pm 03:14 PM

In the field of web development, XML and JSON, one of the data formats, are widely used, and the Gin framework is a lightweight Go language web framework that is simple, easy to use and has efficient performance. This article will introduce how to use the Gin framework to implement XML and JSON data parsing functions. Gin Framework Overview The Gin framework is a web framework based on the Go language, which can be used to build efficient and scalable web applications. The Gin framework is designed to be simple and easy to use. It provides a variety of middleware and plug-ins to make the development

Error handling in Golang: How to handle json parsing errors? Error handling in Golang: How to handle json parsing errors? Aug 07, 2023 pm 02:01 PM

Error handling in Golang: How to handle json parsing errors? In Golang, handling errors is a very important issue. Especially when dealing with external data, such as JSON data returned by network requests, we need to pay special attention to error handling. This article will introduce how to handle JSON parsing errors in Golang, and how to handle these errors gracefully. In Golang, JSON parsing errors are usually caused by incorrect data format or mismatched structure. when we look from the outside

How to parse JSON data from HTTP response in Golang? How to parse JSON data from HTTP response in Golang? Jun 03, 2024 pm 01:26 PM

Parsing JSON responses in Go: Use the Unmarshal function of the encoding/json package. Create a target structure that represents the JSON data. Read the HTTP response body and parse the JSON data. Print or use the parsed data.

Detailed explanation of JSON parsing library in Python Detailed explanation of JSON parsing library in Python Jun 10, 2023 pm 03:37 PM

With the development of the Internet era, data has become the basis of every form of information we are exposed to, and among them, JSON data format is often used in network data exchange. In order to facilitate the parsing and use of this data format, the Python language provides a JSON parsing library, which will be explained in detail in this article. 1. Introduction to JSON JSON (JavaScriptObjectNotation) is a lightweight data exchange format. Compared with XML, JSON is more concise and easier to

How to get different types of values ​​from JSON object in Java? How to get different types of values ​​from JSON object in Java? Aug 21, 2023 am 08:49 AM

A JSONObject is an unordered collection of key-value pairs and parses a text string to produce a map-like object. A JSONObject has several important methods to display different types of values. For example, the getString() method is used to obtain the string associated with the key string, the getInt() method is used to obtain the integer value associated with the key, and the getDouble() method Used to get the double value associated with the key, the getBoolean() method is used to get the Boolean value associated with the key. Example importorg.json.*;publicclassJSONObjectTypeValuesTest{&

A guide to JSON parsing and generation in PHP A guide to JSON parsing and generation in PHP Jun 11, 2023 am 10:57 AM

With the continuous development and application of Internet technology, front-end and back-end data interaction has become a necessary part of Web development. As a lightweight data exchange format, JSON has become one of the most popular choices. As an important language in the field of web development, PHP also provides a complete JSON processing function library, so developers can easily parse and generate JSON data. This article will introduce how to parse and generate JSON in PHP. 1. Basic concepts of JSON JSON (JavaScri

How to parse and generate JSON format in PHP How to parse and generate JSON format in PHP Jul 28, 2023 am 11:53 AM

How to parse and generate JSON format in PHP In modern network development, JSON (JavaScriptObjectNotation) has become a commonly used data exchange format. It is lightweight, easy to read and write, and is widely used in various programming languages. PHP, as a popular server-side scripting language, also provides powerful support for parsing and generating JSON format data. This article will introduce how to parse and generate JSON format in PHP, including using

How to parse JSON to Gson tree model in Java? How to parse JSON to Gson tree model in Java? Aug 27, 2023 pm 05:25 PM

The Gson library can be used to parse JSON strings into tree models. We can use JsonParser to parse a JSON string into a tree model of type JsonElement. The getAsJsonObject() method of JsonElement can be used to obtain JsonObject and the getAsJsonArray() JsonElement method can be used to obtain elements in the form of JsonArray. Syntax publicJsonObjectgetAsJsonObject()publicJsonArraygetAsJsonArray()Example importjava.uti

See all articles