Skip to content

Instantly share code, notes, and snippets.

@raul1991
Created July 3, 2014 05:43
Show Gist options
  • Save raul1991/bad859e48c60f9e3fc62 to your computer and use it in GitHub Desktop.
Save raul1991/bad859e48c60f9e3fc62 to your computer and use it in GitHub Desktop.
Gson programs
import java.util.ArrayList;
import java.util.List;
public class DataModel {
private String title = "GsonDemo";
private int length = 1;
private float temperature = 37.5f;
private List<String> items = new ArrayList<String>() {
{
add("item1");
add("item2");
add("item3");
}
};
@Override
public String toString() {
return "title=" + this.title + "\n" + "length=" + this.length + "\n" + "temperature=" + this.temperature + "items=" + this.items.toString();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.ArrayList;
public class DataModelComplex {
private String status;
private ArrayList<Results> results;
/**
* @return the status
*/
public String getStatus() {
return status;
}
/**
* @param status the status to set
*/
public void setStatus(String status) {
this.status = status;
}
/**
* @return the results
*/
public ArrayList<Results> getResults() {
return results;
}
/**
* @param results the results to set
*/
public void setResults(ArrayList<Results> results) {
this.results = results;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class Geometry {
private Location location;
/**
* @return the location
*/
public Location getLocation() {
return location;
}
/**
* @param location the location to set
*/
public void setLocation(Location location) {
this.location = location;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import pojo.DataModel;
public class GsonBasicJsonRead {
public GsonBasicJsonRead() throws FileNotFoundException {
FileInputStream fileInputStream = new FileInputStream(new File("input.json"));
BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));
Gson gson = new Gson();
DataModel dataModel = gson.fromJson(reader, DataModel.class);
System.out.println("\n--------------------------------------\n");
System.out.println(dataModel.toString());
System.out.println("--------------------------------------");
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import com.google.gson.Gson;
import java.io.FileWriter;
import java.io.IOException;
import pojo.DataModel;
import pojo.DataModelComplex;
public class GsonBasicJsonWrite {
public GsonBasicJsonWrite() throws IOException {
Gson gson =new Gson();
String json=gson.toJson(DataModel.class);
FileWriter fileWriter = new FileWriter("output.json");
fileWriter.write(json);
fileWriter.close();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import pojo.DataModelComplex;
public class GsonComplexJsonRead {
public GsonComplexJsonRead() throws FileNotFoundException{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("input_complex.json")));
Gson gson = new Gson();
DataModelComplex dataModel=gson.fromJson(reader,DataModelComplex.class);
System.out.println("status : "+dataModel.getStatus());
int results = dataModel.getResults().size();
for(int i=0;i<results;i++){
System.out.println("\n---------------Result "+i+"-----------------\n");
System.out.println("formatted_address : "+ dataModel.getResults().get(i).getFormatted_address());
System.out.println("types : "+ dataModel.getResults().get(i).getTypes().get(i));
System.out.println("location : "+ dataModel.getResults().get(i).getGeometry().getLocation().getLat()+","+dataModel.getResults().get(i).getGeometry().getLocation().getLon());
System.out.println("-------------------------------------------\n");
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import com.google.gson.Gson;
import java.io.FileWriter;
import java.io.IOException;
import pojo.DataModelComplex;
public class GsonComplexJsonWrite {
public GsonComplexJsonWrite() throws IOException {
Gson gson =new Gson();
String json=gson.toJson(new DataModelComplex());
FileWriter fileWriter = new FileWriter("output.json");
fileWriter.write(json);
fileWriter.close();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class Location {
private double lat;
private double lng;
/**
* @return the lat
*/
public double getLat() {
return lat;
}
/**
* @param lat the lat to set
*/
public void setLat(double lat) {
this.lat = lat;
}
/**
* @return the lon
*/
public double getLon() {
return lng;
}
/**
* @param lon the lon to set
*/
public void setLon(double lon) {
this.lng = lon;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Scanner;
public class GsonDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
Scanner sc=new Scanner(System.in);
switch(sc.nextInt()){
case 1:
new GsonBasicJsonRead();
break;
case 2:
new GsonBasicJsonWrite();
break;
case 3:
new GsonComplexJsonRead();
break;
case 4:
new GsonComplexJsonWrite();
break;
default:break;
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.ArrayList;
public class Results {
private String formatted_address;
private ArrayList<String> types;
private Geometry geometry;
/**
* @return the formatted_address
*/
public String getFormatted_address() {
return formatted_address;
}
/**
* @param formatted_address the formatted_address to set
*/
public void setFormatted_address(String formatted_address) {
this.formatted_address = formatted_address;
}
/**
* @return the types
*/
public ArrayList<String> getTypes() {
return types;
}
/**
* @param types the types to set
*/
public void setTypes(ArrayList<String> types) {
this.types = types;
}
/**
* @return the geometry
*/
public Geometry getGeometry() {
return geometry;
}
/**
* @param geometry the geometry to set
*/
public void setGeometry(Geometry geometry) {
this.geometry = geometry;
}
}
@raul1991
Copy link
Author

raul1991 commented Jul 3, 2014

This has a complete list of programs for using Gson to read/write basic/complex json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment