Converting JSONArray into CSV file in java
package com.loginController;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONObject;
/**
*
* @author anand
*
*/
public class ConvertArray {
public static void main(String myHelpers[]){
String jsonArrayString = "{\"fileName\": [{\"name\": \"Anand\",\"last\": \"Dwivedi\",\"place\": \"Bangalore\"}]}";
JSONObject output;
try {
output = new JSONObject(jsonArrayString);
JSONArray docs = output.getJSONArray("fileName");
File file=new File("JSONSEPERATOR.csv");
String csv = CDL.toString(docs);
FileUtils.writeStringToFile(file, csv);
System.out.println("Data has been Sucessfully Writeen to "+file);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONObject;
/**
*
* @author anand
*
*/
public class ConvertArray {
public static void main(String myHelpers[]){
String jsonArrayString = "{\"fileName\": [{\"name\": \"Anand\",\"last\": \"Dwivedi\",\"place\": \"Bangalore\"}]}";
JSONObject output;
try {
output = new JSONObject(jsonArrayString);
JSONArray docs = output.getJSONArray("fileName");
File file=new File("JSONSEPERATOR.csv");
String csv = CDL.toString(docs);
FileUtils.writeStringToFile(file, csv);
System.out.println("Data has been Sucessfully Writeen to "+file);
} catch (Exception e) {
e.printStackTrace();
}
}
}
what is the file's location
ReplyDeleteThanks for referring this blog . File location in direct inside your Project . so you can copy JSONSEPERATOR.csv file inside your project
Deletewhat if i want to take the json file instead of directly copying the json data into string?
ReplyDeleteYou have to write the code which reads JSON file content and assign into string then you can parse same string
DeleteCan u plz provide me the code snippet , explaining the same?
ReplyDeleteSushil.. asking the code snipped directly won't help you. Best way is to try something at your end and then ask for help here.Hope you will take it in a very positive context.
Delete