Sample 1:- Using json-lib library
Transforming beans, maps, collections, java arrays and XML to JSON
and back again to beans and DynaBeans.
- Exporting to JSON
import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONObject; public class Export { public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("name", "Mohammed"); map.put("empid", "E-1097"); map.put("phone", "123456789"); JSONObject json = new JSONObject(); json.accumulateAll(map); List<String> list = new ArrayList<String>(); list.add("JAVA"); list.add("JSON"); list.add("JSF"); list.add("HIBERNATE"); json.accumulate("technology", list); System.out.println(json.toString()); try { FileWriter file = new FileWriter("d:\\sample.json"); file.write(json.toString()); file.flush(); file.close(); } catch (IOException e) { e.printStackTrace(); } } }
OUTPUT:- { "name":"Mohammed", "empid":"E-1097", "phone":"123456789", "technology":["JAVA","JSON","JSF","HIBERNATE"] }
- Importing from JSON
import java.io.BufferedReader; import java.io.FileReader; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Import { public static void main(String[] args) { try { FileReader fin = new FileReader("d:\\sample.json"); BufferedReader bin = new BufferedReader(fin); StringBuilder sb = new StringBuilder(); String b; while ((b = bin.readLine()) != null) { sb.append(b); } JSONObject jobj = JSONObject.fromObject(sb.toString()); System.out.println(jobj.get("name")); System.out.println(jobj.get("empid")); System.out.println(jobj.get("phone")); JSONArray jarr = jobj.getJSONArray("technology"); for (int i = 0; i < jarr.size(); i++) System.out.print(jarr.getString(i) + " "); } catch (Exception ex) { ex.printStackTrace(); } } }
OUTPUT:-
Mohammed
E-1097
123456789
JAVA JSON JSF HIBERNATE
Sample 2 :- Using Google Gson library
To
convert Java objects to JSON and vice-versa
- Java Class
package com.sajadhaja.gson; import java.util.List; public class Data { private String title; private int id; private boolean children; private List<Data> groups; public Data(String title, int id, boolean children, List<Data> groups) { this.title = title; this.id = id; this.children = children; this.groups = groups; } public Data(String title, int id, boolean children) { this.title = title; this.id = id; this.children = children; } public String getTitle() { return title; } public int getId() { return id; } public boolean getChildren() { return children; } public List<Data> getGroups() { return groups; } public void setTitle(String title) { this.title = title; } public void setId(int id) { this.id = id; } public void setChildren(boolean children) { this.children = children; } public void setGroups(List<Data> groups) { this.groups = groups; } public String toString() { return String.format("title:%s\n id:%d\n children:%s\n groups:%s", title, id, children, groups); } }
- Exporting to JSON
package com.sajadhaja.gson; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.google.gson.Gson; public class Export { public static void main(String[] args) { try { List<Data> l = new ArrayList<Data>(); List<Data> listnodata = new ArrayList<Data>(); Data dchild = new Data("Child", 122, false, listnodata); l.add(dchild); Data d = new Data("Parent", 111, true, l); String jstr = new Gson().toJson(d); FileWriter file = new FileWriter("d:\\samplegs.json"); file.write(jstr); file.flush(); file.close(); } catch (IOException e) { e.printStackTrace(); } } }
OUTPUT:-
{
"title":"Parent",
"id":111,
"children":true,
"groups":[
{"title":"Child",
"id":122,
"children":false,
"groups":[]
}
]
}
- Importing from JSON
package com.sajadhaja.gson; import java.io.BufferedReader; import java.io.FileReader; import com.google.gson.Gson; public class Import { public static void main(String args[]) { try { FileReader fin = new FileReader("d:\\samplegs.json"); BufferedReader bin = new BufferedReader(fin); StringBuilder sb = new StringBuilder(); String b; while ((b = bin.readLine()) != null) { sb.append(b); } System.out.println(sb.toString()); Data data = new Gson().fromJson(sb.toString(), Data.class); System.out.println(data.toString()); } catch (Exception ex) { ex.printStackTrace(); } } }
OUTPUT:-
title:Parent,id:111,children:true,groups:[title:Child,id:122,children:false,groups:[]]
No comments:
Post a Comment
You can enter queries here...