How to Create JSON object in java ?

In this tutorial, we will learn about How to create JSON object in Java ?

JSON is a language-independent data format. It has been extended from the JavaScript scripting language. The official Internet media type for JSON is application/json. JSON filenames use the extension .json. It is a syntax for storing and exchanging data. JSON is text, written with JavaScript object notation. Data structures in JSON are based on key/value pairs. The key is a string, the value can be a numerical value, a boolean value (true or false) or an object.

We can create JSON objects using JavaScript. Let us see the different ways of creating JSON objects using JavaScript.

Creation of an empty Object:

Creation of a new Object:

Let’s create an object with attribute empName with value in string, attribute age with numeric value. The attributes are accessed by using “.” operator.

Let’s see how to use this jsonObj in HTML using JavaScript.

JSON use in HTML with JavaScript:

Here we will try to use the JSON object created above in HTML using JavaScript. For this we will create a JSONObjectDemo.htm with below code snippet.

Let’s open JSONObjectDemo.htm using IE or any other JavaScript enabled browser. It will generate the following output:

JSON with JavaScript example

Employee Name = Roger

Age= 35

You have successfully used JSON object in HTML using JavaScript.

In this tutorial, we have seen how to create JSON object in Java.

Related posts

Leave a Comment