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();
        }    
    }

}

Comments

  1. what is the file's location

    ReplyDelete
    Replies
    1. Thanks for referring this blog . File location in direct inside your Project . so you can copy JSONSEPERATOR.csv file inside your project

      Delete
  2. what if i want to take the json file instead of directly copying the json data into string?

    ReplyDelete
    Replies
    1. You have to write the code which reads JSON file content and assign into string then you can parse same string

      Delete
  3. Can u plz provide me the code snippet , explaining the same?

    ReplyDelete
    Replies
    1. Sushil.. 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

Post a Comment

Popular posts from this blog

Online Book Store Website Using Java

How to read CSV File in Java