Found 2616 Articles for Java

How to get the values of a key using the JsonPointer interface in Java?

raja
Updated on 07-Jul-2020 11:58:26

581 Views

The JSONPointer is a standard that defines a string syntax that can be used to access a particular key value in the JSON document. An instance of JSONPointer can be created by calling the static factory method createPointer() on the Json class. In the JSONPointer,  every string syntax is prefixed with “/”. We can get the value of a key by calling the getValue() method on the JsonPointer object.JSON fileExampleimport javax.json.*; import java.io.*; public class JsonPointerTest {    public static void main(String[] args) throws Exception {       JsonReader jsonReader = Json.createReader(new FileReader("simple.json"));       JsonStructure jsonStructure = jsonReader.read();       JsonPointer jsonPointer1 ... Read More

Importance of the JsonPatch interface in Java?

raja
Updated on 07-Jul-2020 11:55:50

483 Views

The JsonPatch interface is a format for storing a sequence of operations that can be applied to the target JSON structure. There are few operations like add, remove, replace, copy, move and test can be stored in JsonPath and operated on JSON structure. The JsonPatchBuilder interface can be used for constructing a JSON patch using the Json.createPatchBuilder().JSON fileExampleimport java.io.*; import javax.json.Json; import javax.json.JsonPatch; import javax.json.JsonPatchBuilder; import javax.json.JsonReader; import javax.json.JsonStructure; public class JsonPatchTest {    public static void main(String[] args) throws Exception {       JsonPatchBuilder jsonPatchBuilder = Json.createPatchBuilder();       JsonPatch jsonPatch = jsonPatchBuilder.add("/postalCode", "500072").remove("/age").build();       JsonReader reader = Json.createReader(new FileReader("simple.json"));     ... Read More

How to create a JSON using JsonObjectBuilder and JsonArrayBuilder in Java?

raja
Updated on 07-Jul-2020 11:43:40

7K+ Views

The JsonObjectBuilder can be used for creating JsonObject models whereas the JsonArrayBuilder can be used for creating JsonArray models. The JsonObjectBuilder can be created using the Json class, it contains methods to create the builder object and build an empty JsonObject instance using the Json.createObjectBuilder().build(). The JsonArrayBuilder can be created using the Json class, it contains methods to create the builder object and build an empty JsonArray instance using Json.createArrayBuilder().build().Exampleimport java.io.*; import javax.json.*; public class JsonObjectTest {    public static void main(String[] args) {       JsonObject empObject = Json.createObjectBuilder().add("empName", "Jai")                                  .add("empAge", ... Read More

How can we implement a JSON array using Streaming API in Java?

raja
Updated on 18-Feb-2020 10:47:33

817 Views

The JsonGenerator interface can be used to write the JSON data to an output source in a streaming way. We can create or implement a JSON array using the writeStartArray() method of JsonGenerator, this method writes the JSON name/start array character pair within the current object context. The writeStartObject() method writes the JSON start object character, and valid only in an array context and the writeEnd() method writes the end of the current context.SyntaxJsonGenerator writeStartArray(String name)Exampleimport java.io.*; import javax.json.*; import javax.json.stream.*; public class JsonGeneratorTest {    public static void main(String[] args) throws Exception {       StringWriter writer = new ... Read More

Pretty print JSON using javax.json API in Java?

raja
Updated on 07-Jul-2020 07:58:47

710 Views

The javax.json package provides an Object Model API to process JSON. The Object Model API is a high-level API that provides immutable object models for JSON object and array structures. These JSON structures can be represented as object models using JsonObject and JsonArray interfaces. We can use the JsonGenerator interface to write the JSON data to an output in a streaming way. The JsonGenerator.PRETTY_PRINTING is a configuration property to generate JSON prettily.We can implement a pretty print JSON in the below example.Exampleimport java.io.*; import java.util.*; import javax.json.*; import javax.json.stream.*; public class JSONPrettyPrintTest {    public static void main(String args[]) {       String jsonString = ... Read More

How to parse a JSON string using Streaming API in Java?

raja
Updated on 07-Jul-2020 07:40:21

2K+ Views

The Streaming API consists of an important interface JsonParser and this interface contains methods to parse JSON in a streaming way and provides forward, read-only access to JSON data. The Json class contains the methods to create parsers from input sources. We can parse a JSON using the static method createParser() of Json class.Syntaxpublic static JsonParser createParser(Reader reader)Exampleimport java.io.*; import javax.json.Json; import javax.json.stream.JsonParser; import javax.json.stream.JsonParser.Event; public class JSONParseringTest {    public static void main(String[] args) {       String jsonString = "{\"name\":\"Adithya\", \"employeeId\":\"115\", \"age\":\"30\"}";       JsonParser parser = Json.createParser(new StringReader(jsonString));       while(parser.hasNext()) {          Event event = ... Read More

How to create a JSON Object using Object Model in Java?

raja
Updated on 07-Jul-2020 07:25:17

2K+ Views

The javax.json.JsonObject interface can represent an immutable JSON object value and provides an unmodifiable map view to the JSON object name/value mappings. A JsonObject instance can be created from an input source using the static method readObject() of javax.json.JsonReader class and also can be created using the static method createObjectBuilder() of javax.json.Json class.Syntaxpublic static JsonObjectBuilder createObjectBuilder()Exampleimport java.io.*; import javax.json.*; public class JsonObjectTest {    public static void main(String[] args) throws Exception {       JsonObjectBuilder builder = Json.createObjectBuilder();       builder.add("Name", "Adithya");       builder.add("Designation", "Python Developer");       builder.add("Company", "TutorialsPoint");       builder.add("Location", "Hyderabad");       JsonObject data = builder.build();     ... Read More

How to create a JSON Array using Object Model in Java?

raja
Updated on 07-Jul-2020 07:09:51

1K+ Views

The javax.json.JsonArray interface can represent an immutable JSON array and provides an unmodifiable list view of the values in the array. A JsonArray object can be created by reading JSON data from an input source and also using a static method createArrayBuilder() of javax.json.Json class. We need to import the javax.json package (download javax.json-api.jar file) in order to execute it.Syntaxpublic static JsonArrayBuilder createArrayBuilder()Exampleimport java.io.*; import javax.json.*; import javax.json.JsonObjectBuilder; public class JsonArrayTest {    public static void main(String[] args) {       JsonObjectBuilder builder = Json.createObjectBuilder();       builder.add("Name", "Raja Ramesh");       builder.add("Designation", "Java Developer");       builder.add("Company", "TutorialsPoint");       ... Read More

How to deserialize a Java object from Reader Stream using flexjson in Java?

raja
Updated on 07-Jul-2020 06:25:10

280 Views

The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format. We can deserialize a Java object from a Reader stream using the deserialize() method of JSONDeserializer class, it uses an instance of Reader class as JSON input.Syntaxpublic T deserialize(Reader input)Exampleimport java.io.*; import flexjson.JSONDeserializer; public class JSONDeserializeReaderTest {    public static void main(String[] args) {       JSONDeserializer deserializer = new JSONDeserializer();       String jsonStr =                        "{" +                         "\"firstName\": \"Adithya\", " + ... Read More

Importance of the accumulate() method of JSONObject in Java?

raja
Updated on 07-Jul-2020 05:54:35

1K+ Views

A JSONObject is an unordered collection of a name and value pairs. A few important methods of JSONArray are accumulate(), put(), opt(), append(), write() and etc. The accumulate() method accumulates the values under a key and this method similar to the put() method except if there is an existing object stored under a key then a JSONArray can be stored under a key to hold all of the accumulated values. If there is an existing JSONArray then a new value can be added.Syntaxpublic JSONObject accumulate(java.lang.String key, java.lang.Object value) throws JSONExceptionExampleimport org.json.*; public class JSONAccumulateMethodTest {    public static void main(String[] args) throws JSONException { ... Read More

Advertisements