The Flexjson library is a lightweight Java library for serializing and de-serializing java beans, maps, arrays, and collections in a JSON format. A JSONSerializer is the main class for performing serialization of Java objects to JSON and by default performs a shallow serialization. We can wrap a JSON object using the rootName() method of JSONSerializer class, this method wraps the resulting JSON in a javascript object that contains a single field named rootName.Syntaxpublic JSONSerializer rootName(String rootName)Exampleimport flexjson.JSONSerializer; public class JSONRootNameTest { public static void main(String[] args) { JSONSerializer serializer = new JSONSerializer().rootName("My_Employee").prettyPrint(true); Employee emp = new Employee("Adithya", "Jai", 28, ... Read More
We can convert a factor to integer or numeric variable by using as.numeric function with defining the levels of the factor or by defining the characters of the factorExample> f f [1] 0.323049098020419 0.916131897130981 0.271536672720686 0.462429489241913 [5] 0.657008627429605 0.462429489241913 0.462429489241913 0.212830029195175 [9] 0.271536672720686 0.497305172728375 7 Levels: 0.212830029195175 0.271536672720686 ... 0.916131897130981Using as.numeric> as.numeric(levels(f))[f] [1] 0.3230491 0.9161319 0.2715367 0.4624295 0.6570086 0.4624295 0.4624295 [8] 0.2128300 0.2715367 0.4973052 > Using as.numeric(as.character( )) > as.numeric(as.character(f)) [1] 0.3230491 0.9161319 0.2715367 0.4624295 0.6570086 0.4624295 0.4624295 [8] 0.2128300 0.2715367 0.4973052
We can replace all NA values by using is.na functionExample> Data df df V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 1 9 7 3 0 3 7 7 3 9 9 2 9 2 3 0 2 0 1 4 6 7 3 5 0 9 2 4 8 8 7 NA 5 4 7 3 1 2 6 NA 7 1 1 8 5 3 2 9 6 4 7 0 5 6 1 6 8 5 6 5 3 9 6 0 7 0 7 8 3 4 NA NA 0 2 4 2 NA 8 6 9 9 9 4 0 6 1 7 NA 9 5 5 NA 8 1 NA 0 9 9 3 10 1 1 0 7 1 1 4 1 2 1Replacing NA’s by 0’s> df[is.na(df)] df V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 1 9 7 3 0 3 7 7 3 9 9 2 9 2 3 0 2 0 1 4 6 7 3 5 0 9 2 4 8 8 7 0 5 4 7 3 1 2 6 0 7 1 1 8 5 3 2 9 6 4 7 0 5 6 1 6 8 5 6 5 3 9 6 0 7 0 7 8 3 4 0 0 0 2 4 2 0 8 6 9 9 9 4 0 6 1 7 0 9 5 5 0 8 1 0 0 9 9 3 10 1 1 0 7 1 1 4 1 2 1
Columns of a data frame can be dropped by creating an object of columns that we want to drop or an object of columns that we want to keep.Example> df keeps df[keeps] Var3 Var4 1 21 31 2 22 32 3 23 33 4 24 34 5 25 35 6 26 36 7 27 37 8 28 38 9 29 39 10 30 40
We can use regression model object name with $r.squared to find the R-squared and a user defined function to extract the p-value.ExampleExtracting R-Squared> x y LinearRegression summary(LinearRegression)$r.squared [1] 0.2814271Extracting p-value> Regressionp
We can sort a data frame by multiple columns using order function.ExampleConsider the below data frame −> df df x1 x2 x3 x4 1 Hi A 4 9 2 Med B 7 5 3 Hi D 5 7 4 Low C 3 4Let’s say we want to sort the data frame by column x4 in descending order then by column x1 in ascending order.It can be done follows −> df[with(df, order(-x4, x1)), ] x1 x2 x3 x4 1 Hi A 4 9 3 Hi D 5 7 2 Med B 7 5 4 Low C 3 4We can do ... Read More
The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format. We can serialize a list of objects using the serialize() method of JSONSerializer class. This method can perform a shallow serialization of the target instance. We need to pass a list of objects of List type as an argument to the serialize() method.Syntaxpublic String serialize(Object target)Exampleimport flexjson.JSONSerializer; import java.util.*; public class JsonSerializeListTest { public static void main(String[] args) { JSONSerializer serializer = new JSONSerializer().prettyPrint(true); // pretty print JSON Student s1 = new Student("Raja", "Ramesh", 28, "Hyderabad"); Student s2 = new Student("Suresh", "Kumar", 30, "Chennai"); ... Read More
The @JsonAnyGetter annotation enables to use a Map as a container for properties that we want to serialize to JSON and @JsonAnySetter annotation instructs Jackson to call the same setter method for all unrecognized fields in the JSON object, which means that all fields that are not already mapped to a property or setter method in the Java object.Syntaxpublic @interface JsonAnyGetter public @interface JsonAnyGetterExampleimport java.io.*; import java.util.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.annotation.*; public class JsonAnyGetterAndJsonAnySetterTest { public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException { Employee emp1 = new Employee(); emp1.setFirstName("Adithya"); emp1.setLastName("Sai"); ... Read More
The Jackson API provides a number of methods to work with JSON data. By using Jackson API, we can convert Java objects to JSON string and reform the object from the JSON string. We can implement a custom serializer using the StdSerializer class and need to override the serialize(T value, JsonGenerator gen, SerializerProvider provider) method, the first argument value represents value to serialize(can not be null), the second argument gen represents generator used to output resulting Json content and the third argument provider represents provider that can be used to get serializers for serializing objects value.Syntaxpublic abstract void serialize(T value, JsonGenerator gen, SerializerProvider provider) throws IOExceptionExampleimport java.io.*; ... Read More
A Jackson is a Java JSON API that provides several different ways to work with JSON. We can convert CSV data to JSON data using the CsvMapper class, it is specialized ObjectMapper, with extended functionality to produce CsvSchema instances out of POJOs. We can use the reader() method for constructing ObjectReader with default settings. In order to convert this, we need to import the com.fasterxml.jackson.dataformat.csv package.In the below example, convert a CSV to JSON.Exampleimport java.io.*; import java.util.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.dataformat.csv.*; public class CsvToJsonTest { public static void main(String args[]) throws Exception { File input = new File("input.csv"); try { ... Read More