Found 208 Articles for JSON

How can we write JSON objects to a file in Java?

raja
Updated on 04-Jul-2020 05:35:31

2K+ Views

The JSON is one of the widely used data-interchange formats and is a lightweight and language independent. The json.simple is a lightweight JSON processing library that can be used to write JSON files and it can be used to encode or decode JSON text and fully compliant with JSON specification(RFC4627). In order to read a JSON file, we need to download the json-simple.jar file and set the path to execute it.Exampleimport java.io.*; import java.util.*; import org.json.simple.*; import org.json.simple.parser.*; public class JSONObjectWriterToFileTest {    public static void main(String[] args) throws IOException {       JSONObject obj = new JSONObject();       obj.put("Name", "Adithya");       ... Read More

How can we add a JSONArray to JSONObject in Java?

raja
Updated on 04-Jul-2020 05:36:04

19K+ Views

The JSON is a text-based format for exchanging data. It is a lightweight component and language independent. We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method. Exampleimport org.json.*; import java.util.*; public class AddJSONArrayToJSONObjTest {    public static void main(String args[]) {       List list = new ArrayList();       list.add("Raja");       list.add("Jai");       list.add("Adithya");       JSONArray array = new JSONArray();       for(int i = 0; i < list.size(); ... Read More

How can we read a JSON file in Java?

raja
Updated on 02-Sep-2023 12:55:51

61K+ Views

The JSON is one of the widely used data-interchange formats and is a lightweight and language independent. The json.simple is a lightweight JSON processing library that can be used to read and write JSON files and it can be used to encode or decode JSON text and fully compliant with JSON specification (RFC4627). In order to read a JSON file, we need to download the json-simple.jar file and set the path to execute it.json fileExampleimport java.io.*; import java.util.*; import org.json.simple.*; import org.json.simple.parser.*; public class JSONReadFromTheFileTest {    public static void main(String[] args) {       JSONParser parser = new JSONParser();       try {     ... Read More

How can we convert a JSON string to a JSON object in Java?

raja
Updated on 03-Jul-2020 14:06:49

6K+ Views

The JSON stands for JavaScript Object Notation and it can be used to transfer and storage of data.  The JSONObject can parse text from a String to produce a map-like object. The object provides methods for manipulating its contents, and for producing a JSON compliant object serialization. The JSONArray can parse text from a String to produce a vector-like object. The object provides methods for manipulating its contents, and for producing a JSON compliant array serialization.In the below two examples, We can convert a JSON string to a JSON object.Example 1import org.json.JSONObject; import org.json.JSONArray; public class StringToJSONTest {    public static void main(String args[]) {   ... Read More

How to convert HASHMAP to JSON using GSON in Android?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

1K+ Views

GSON is java library, It is used to convert OBJECT to JSON and JSON to Object. Internally it going to work based on serialization and deserialization.This example demonstrates how to convert HASHAMP to JSON using GSON library.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code in build.gradle.apply plugin: 'com.android.application' android {    compileSdkVersion 28    defaultConfig {       applicationId "com.example.andy.myapplication"       minSdkVersion 15       targetSdkVersion 28       versionCode ... Read More

How to store data in MySQL as JSON?

George John
Updated on 30-Jul-2019 22:30:23

305 Views

We can store data in MySQL as JSON with the help of JSON data type. The following is an example. Let us now create a table. mysql> CREATE table JsonAsMySQLDemo -> ( -> id int, -> name varchar(100), -> PageDemo JSON, -> eventInfo JSON -> ); Query OK, 0 rows affected (0.67 sec) Storing records into JSON data type. mysql> INSERT into JsonAsMySQLDemo values -> ( -> 1, ... Read More

Map.delete() function in JavaScript

karthikeya Boyini
Updated on 25-Jun-2020 12:14:47

6K+ Views

The delete() function of Map object accepts a string representing the key of an element of a map and deletes from the current Map object. This function returns true if the specified key exists in the map object else it returns false.SyntaxIts Syntax is as followsmapVar.delete()Example Live Demo    JavaScript Example           var mapVar = new Map();       mapVar.set('1', 'Java');       mapVar.set('2', 'JavaFX');       mapVar.set('3', 'HBase');       mapVar.set('4', 'Neo4j');       var map = mapVar.delete('4');       document.write("Size of the map object: "+mapVar.size);   ... Read More

JSON. stringify( ) function in JavaScript

karthikeya Boyini
Updated on 25-Jun-2020 12:03:07

262 Views

The stringify() function of a JSON object accepts a JSON string, and constructs an object based on the given text and, returns it.SyntaxIts Syntax is as followsJson.stringify();Example Live Demo    JavaScript Example           var jsonSample = '{Tutorial: Java, Version:8}';       jsonObj = JSON.stringify(jsonSample);       document.write(jsonObj);     Output"{Tutorial: Java, Version:8}"

How to generate JSON output using Python?

karthikeya Boyini
Updated on 05-Mar-2020 10:16:33

2K+ Views

The json module in python allows you to dump a dict to json format directly. To use it,Exampleimport json my_dict = {    'foo': 42,    'bar': {       'baz': "Hello",       'poo': 124.2    } } my_json = json.dumps(my_dict) print(my_json)OutputThis will give the output −'{"foo": 42, "bar": {"baz": "Hello", "poo": 124.2}}'You can also pass indent argument to prettyprint the json. exampleimport json my_dict = {    'foo': 42,    'bar': {       'baz': "Hello",       'poo': 124.2    } } my_json = json.dumps(my_dict, indent=2) print(my_json)OutputThis will give the output −{    "foo": 42,    "bar":    {       "baz": "Hello",       "poo": 124.2    } }

Escaping/encoding single quotes in JSON encoded HTML5 data attributes

George John
Updated on 30-Jul-2019 22:30:22

4K+ Views

To escape single quotes, use json_encode() to echo arrays in HTML5 data attributes.printf('', htmlspecialchars(json_encode(array('html5', ...)), ENT_QUOTES, 'UTF-8'));Or you can also using built-injson_encode(array('html5', ...), JSON_HEX_APOS)

Advertisements