Tuesday, October 9, 2012

Create JSON Object and Retrive Data

I Have created a JSON Object and saved it into database then i am getting the it from database as a String and again converting it into as a Java Object. 1- How we will create a JSON object from String
List mylist; //Data is something like this [Sub Type, F.P., P.P., Process,Status]   
 
JSONObject json = new JSONObject();
  json.accumulate("COLUMN", mylist);
String jsonObjString = json.toString();
   
2-If you saved above JSON Object into database and retrieving it again You will get data something like i have commented below...And you can retirve it by Below code
String jsonString;//Data will something like this {"COLUMN":["Sub Type","F.P.","P.P.","Process","Status"]}

 JSONObject jsonObj= new JSONObject();
   jsonObj = jsonObj.fromObject(jsonString);
   JSONArray columnName = (JSONArray) obj.get("COLUMN");

   for (int i = 0; i < columnName.size(); i++) {
    System.out.println(columnName.getString(i));
     
   }
NOTE:- I used net.sf.json Jar

No comments:

Post a Comment