tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 JSON > JSON JAVA > CSV To JSONArray

CSV To JSONArray 

JSON (JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange. Douglas Crockford has provided a reference implementation of JSON in Java at http://json.org/ useful for JSON serialization and deserialization. Click here to download the compiled library. The following example shows converting a CSV to JSONArray.

File Name  :  
com/bethecoder/tutorials/json_java/tests/CSV2Json.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.json_java.tests;

import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONException;

public class CSV2Json {

  /**
   @param args
   @throws JSONException 
   */
  public static void main(String[] argsthrows JSONException {
    String csv = "lastName,dob,age,hobby,firstName\n" 
          "Kasireddi,Thu May 06 00:00:00 IST 2010,2,Singing,Sriram\n" 
          "Kasireddi,Mon Sep 06 00:00:00 IST 1982,29,Painting,Sudhakar";
    
    JSONArray array = CDL.toJSONArray(csv);
    System.out.println(array.toString(2))//pretty print with indent
  }

}
   

It gives the following output,
[
  {
    "lastName": "Kasireddi",
    "age": "2",
    "dob": "Thu May 06 00:00:00 IST 2010",
    "hobby": "Singing",
    "firstName": "Sriram"
  },
  {
    "lastName": "Kasireddi",
    "age": "29",
    "dob": "Mon Sep 06 00:00:00 IST 1982",
    "hobby": "Painting",
    "firstName": "Sudhakar"
  }
]



 
  


  
bl  br