Posted In: Jackson 2X, JSON

Converting CSV to JSON using Jackson library

Maven Jackson 2X Classes used

Read More

Tags:

by , on August 31st, 2017

Posted In: JSON, XML

How to convert XML to JSON using org.json.XML

Result

Read More

Tags:

by , on August 31st, 2017

Posted In: Java Core, JSON

Converting JSON to CSV in Java using Arkni – json-to-csv

Download org.jsontocsv.parser.JSONFlattener from Arkni – json-to-csv package com.example.csv; import java.util.List; import java.util.Map; import org.jsontocsv.parser.JSONFlattener; import org.jsontocsv.writer.CSVWriter; import org.junit.Test; public class JsonToCsv2 { @Test public void jsontocsv() throws Exception { System.out.println(“jsontocsv started”); try { List flatJson = JSONFlattener .parseJson(jsonString()); CSVWriter.writeToFile(CSVWriter.getCSV(flatJson, “,”), “E:/files/outcsv1.csv”); } catch (Exception e) { e.printStackTrace(); } } private static String jsonString() { return […]

Read More

Tags:

by , on August 31st, 2017

Posted In: Java Core, JSON

Converting JSON to CSV in Java using org.json.CDL

Example first reads file and the creates JSONArray. After that it again creates CSV string and writes to a CSV file. package com.example.csv; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import org.apache.commons.io.FileUtils; import org.json.CDL; import org.json.JSONArray; import org.junit.Test; public class JsonToCsv { private StringBuilder sb = new StringBuilder(); @Test public void jsontocsv() throws Exception […]

Read More

Tags:

by , on August 31st, 2017

Posted In: Java Core

How to read and parse CSV file using Scanner

Example will not work with elements with comma inside quotes package com.example.csv; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import org.junit.Test; public class CvsScanner { @Test public void parser1() throws Exception { System.out.println(“\nparser1 started”); String filepath= “E:/files/cm24AUG2017bhav.csv”; Scanner scanner = null; try { File file = new File(filepath); scanner = new Scanner(file); scanner.useDelimiter(“,”); while (scanner.hasNext()) { […]

Read More

Tags:

by , on August 31st, 2017

Posted In: Java Core, Security

How to generate a random alpha-numeric string

Creating a random string with A-Z and 0-9 Techniques used 1. Apache Commons RandomStringUtils 2. SecureRandom and com.google.common.io.BaseEncoding 3. UUID.randomUUID() 4. https://www.random.org/strings/ In all these methods if you are using these strings for security and do not to take any chance with uniqueness then I suggest store it in DB with unique constraint or add […]

Read More

by , on August 30th, 2017

Posted In: Java Core, String

How to parse a mathematical expression given as a string – using ScriptEngine

Evaluating a math expression given in string form package com.example.mathexpression; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import org.junit.Test; public class MathExpression2 { @Test public void evaluate1() throws Exception { System.out.println(“\nevaluate1() start”); ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName(“JavaScript”); String result = “5+4*(7-15)”; System.out.println(engine.eval(result)); } }

Read More

Tags:

by , on August 30th, 2017

Posted In: Java Core, String

How to parse a mathematical expression given as a string – using Bean Shell

Evaluating a math expression given in string form Maven beanshell beanshell import package com.example.mathexpression; import org.junit.Test; import bsh.Interpreter; public class MathExpression { @Test public void evaluate1() throws Exception { System.out.println(“\nevaluate1() start”); Interpreter interpreter = new Interpreter(); interpreter.eval(“result = 5+4*(7-15)”); System.out.println(interpreter.get(“result”)); } }

Read More

Tags:

by , on August 30th, 2017

Posted In: Java Core, String

How to read and parse CSV file using Guava Splitter

Splitting a comma-separated string but ignoring commas in quotes Maven guava Guava Splitter Works only without commas in quotes data Works with commas in quotes data

Read More

Tags:

by , on August 29th, 2017

  • Categories