Found 206 Articles for JSON

How to convert a bean to JSON object using Exclude Filter in Java?

raja
Updated on 08-Jul-2020 11:20:06

348 Views

The JsonConfig class can be used to configure the serialization process. We can use the setJsonPropertyFilter() method of JsonConfig to set the property filter when serializing to JSON. We need to implement a custom PropertyFilter class by overriding the apply() method of the PropertyFilter interface. It returns true if the property will be filtered out or false otherwise.Syntaxpublic void setJsonPropertyFilter(PropertyFilter jsonPropertyFilter)Exampleimport net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.util.PropertyFilter; public class ConvertBeanToJsonExcludeFilterTest {    public static void main(String[] args) {       Student student = new Student("Sai", "Chaitanya", 20, "Hyderabad");       JsonConfig jsonConfig = new JsonConfig();       jsonConfig.setJsonPropertyFilter(new CustomPropertyFilter());       ... Read More

How to convert a bean to XML without type hints using JSON-lib API in Java?

raja
Updated on 08-Jul-2020 11:20:50

176 Views

The JSON-lib is a Java library for serializing and de-serializing java beans, maps, arrays, and collections in JSON format. We can convert a bean to XML without type hints using the setTypeHintsEnabled() method of XMLSerializer class, this method sets whether JSON types can be included as attributes. We can pass false as an argument to this method to disable the type hints in XML.Syntaxpublic void setTypeHintsEnabled(boolean typeHintsEnabled)Exampleimport net.sf.json.JSONObject; import net.sf.json.xml.XMLSerializer; public class ConvertBeanToXMLNoHintsTest {    public static void main(String[] args) {       Employee emp = new Employee("Krishna Vamsi", 115, 30, "Java");       JSONObject jsonObj = JSONObject.fromObject(emp);     ... Read More

How to convert a bean to XML using JSON-lib API in Java?

raja
Updated on 08-Jul-2020 11:16:03

197 Views

The net.sf.json.xml.XMLSerializer class is a utility class for transforming JSON to XML. When transforming JSONObject instance to XML, this class can add hints for converting back to JSON. We can use the write() method of XMLSerializer class to write a JSON value into an XML string with UTF-8 encoding and it can return a string representation of a well-formed XML document.Syntaxpublic String write(JSON json)Exampleimport net.sf.json.JSONObject; import net.sf.json.xml.XMLSerializer; public class ConvertBeanToXMLTest {    public static void main(String[] args) {       Student student = new Student("Sai", "Adithya", 25, "Pune");       JSONObject jsonObj = JSONObject.fromObject(student);       System.out.println(jsonObj.toString(3)); //pretty print JSON ... Read More

How to convert an array to JSON Array using JSON-lib API in Java?

raja
Updated on 19-Feb-2020 07:46:28

364 Views

A Java array is an object which stores multiple variables of the same type, it can hold primitive types and object references whereas JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values, an internal form is an object having get() and opt() methods for accessing the values by index and element() method for adding or replacing values. In the first step, we can create an Object[] array and pass this parameter as an argument to the toJSON() of JSONSerializer class and typecasting it to get the JSON array.We can convert Object[] array to JSONArray in the below exampleExampleimport ... Read More

How to convert bean to JSON object by excluding some properties using JsonConfig in Java?

raja
Updated on 08-Jul-2020 09:05:50

846 Views

The JsonConfig class is a utility class that helps to configure the serialization process. We can convert a bean to a JSON object with few properties that can be excluded using the setExcludes() method of JsonConfig class and pass this JSON config instance to an argument of static method fromObject() of JSONObject.Syntaxpublic void setExcludes(String[] excludes)In the below example, we can convert bean to a JSON object by excluding some of the properties.Exampleimport net.sf.json.JSONObject; import net.sf.json.JsonConfig; public class BeanToJsonExcludeTest {    public static void main(String[] args) {       Student student = new Student("Raja", "Ramesh", 35, "Madhapur");       JsonConfig jsonConfig = new ... Read More

How to convert a Map to JSON object using JSON-lib API in Java?

raja
Updated on 08-Jul-2020 08:20:25

5K+ Views

A JSONObject is an unordered collection of name/value pairs whereas Map is an object that maps keys to values. A Map cannot contain duplicate keys and each key can map to at most one value. We need to use the JSON-lib library for serializing and de-serializing a Map in JSON format. Initially, we can create a POJO class and pass this instance as an argument to the put() method of Map class and finally add this map instance to the accumulateAll() method of JSONObject.Syntaxpublic void accumulateAll(Map map)In the below example, we can convert Map to a JSON object.Exampleimport java.util.*; import net.sf.json.JSONObject; public class ConvertMapToJSONObjectTest { ... Read More

How to convert a Collection to JSON Array using JSON-lib API in Java?

raja
Updated on 08-Jul-2020 07:30:31

607 Views

The net.sf.json.JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values and an internal form is an object having get() and opt() methods for accessing the values by index, and element() method for adding or replacing values. The values can be any of these types like Boolean, JSONArray, JSONObject, Number, String and JSONNull object.We can convert a collection(List) to JSON array in the below exampleExampleimport java.util.*; import net.sf.json.JSONArray; import net.sf.json.JSONSerializer; public class ConvertCollectionToJsonArrayTest {    public static void main(String[] args) {       List strList = Arrays.asList("India", "Australia", "England", ... Read More

How to add elements to JSON Object using JSON-lib API in Java?

raja
Updated on 08-Jul-2020 07:31:03

2K+ Views

The JSON-lib is a Java library for serializing and de-serializing java beans, maps, arrays, and collections in JSON format. We can add elements to the JSON object using the element() method of JSONObject class. We need to download all the dependent jars like json-lib.jar, ezmorph.jar, commons-lang.jar, commons-collections.jar, commons-beanutils.jar, and commons-logging.jar and can import net.sf.json package in our java program to execute it.Syntaxpublic JSONObject element(String key, Object value) - put a key/value pair in the JSONObject Exampleimport java.util.Arrays; import net.sf.json.JSONObject; public class JsonAddElementTest {    public static void main(String[] args) {       JSONObject jsonObj = new JSONObject()          .element("name", "Raja ... Read More

How can we update an existing JSON data using javax.json API in Java?

raja
Updated on 08-Jul-2020 06:48:13

6K+ Views

The JsonBuilderFactory interface is a factory to create JsonObjectBuilder instance and JsonObjectBuilder is a builder for creating JsonObject models from scratch. This interface initializes an empty JSON object model and provides methods to add name/value pairs to the object model and to return the resulting object. We can create a JsonObjectBuilder instance that can be used to build JsonObject using the createObjectBuilder() method.SyntaxJsonObjectBuilder createObjectBuilder()In the below example, We can update an existing JSON data with newly added data.Exampleimport java.io.*; import javax.json.*; public class UpdateExistingJsonTest {    public static void main(String[] args) throws Exception {       String jsonString = "{\"id\":\"115\", \"name\":\"Raja\", \"address\":[{\"area\":\"Madhapur\", \"city\":\"Hyderabad\"}]}";       ... Read More

How to serialize and deserialize a JSON using the ExclusionStrategy interface in Java?

raja
Updated on 08-Jul-2020 06:48:57

570 Views

The ExclusionStrategy interface can be used to exclude any field during serialization and deserialization. We can provide a custom implementation of the ExclusionStrategy interface and need to register it with GsonBuilder using the setExclusionStrategies() method. It configures Gson to apply a set of exclusion strategies during serialization and deserialization.Syntaxpublic GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies)Exampleimport com.google.gson.*; import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; public class ExclusionStrategyTest {    public static void main(String args[]) throws Exception {       Gson gson = new GsonBuilder().setExclusionStrategies(new CustomExclusionStrategy()).create();          Person person = new Person();          person.setFirstName("Adithya");          person.setLastName("Sai");          person.setAddress("Hyderabad");     ... Read More

Previous 1 ... 6 7 8 9 10 ... 21 Next
Advertisements