I have a .dat file that contains some type of products. I'm reading the .dat file and then writing it as JSON and XML files. I'll explain the bug in pic after the files. Here's the problem, in XML file, there's no productCode while in JSON there is. I don't know what's wrong with productCode in XML since I'm passing super(productCode) in both products.
What's wrong with this code?
public static void xmlConverter(List<Product> products) {
XStream xstream = new XStream();
File xmlOutput = new File("data/Products.xml");
PrintWriter xmlPrintWriter = null;
try {
xmlPrintWriter = new PrintWriter(xmlOutput);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
xmlPrintWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> \n <products>");
xstream.alias("product", Product.class);
xstream.alias("refreshment", Refreshment.class);
xstream.alias("parkingpass", ParkingPass.class);
for (Product aProduct: products) {
String productOutput = xstream.toXML(aProduct);
xmlPrintWriter.write( "\n" + productOutput);
}
xmlPrintWriter.write("\n");
xmlPrintWriter.write("</products>");
xmlPrintWriter.close();
}
Products.dat
6
ff23;R;Labatt Beer-20oz;4.99
90fa;P;25.00
3289;P;55.00
32f4;R;Caramel Popcorn;5.50
90fb;P;20.00
yp23;R;Double Cheeseburger;9.00
Product.java
package Products;
public abstract class Product {
private String productCode;
public Product(String productCode) {
super();
this.productCode = productCode;
}
public String getProductCode() {
return this.productCode;
}
public void setProductCode(String productCode) {
this.productCode = productCode;
}
public abstract String getType();
}
ParkingPass.java
package Products;
public class ParkingPass extends Product {
private double cost;
public ParkingPass(String productCode, double cost) {
super(productCode);
this.cost = cost;
}
public double getParkingFee() {
return cost;
}
public void setParkingFee(double cost) {
this.cost = cost;
}
#Override
public String getType() {
return "P";
}
}
DataConverter.java
import java.io.*;
import java.util.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.thoughtworks.xstream.XStream;
import Products.ParkingPass;
import Products.Product;
import Products.Refreshment;
public class DataConverter {
public static List<Product> readProduct() {
List<Product> productList = new ArrayList<Product>();
Scanner scnr = null;
try {
scnr = new Scanner(new File("data/Products.dat"));
scnr.nextLine();
Product p = null;
while (scnr.hasNext()) {
String line = scnr.nextLine();
String data[] = line.split(";");
String productCode = data[0];
String productType = data[1];
String name = null;
double cost = 0.0;
double parkingFee = 0.0;
if (data.length == 4) {
name = data[2];
cost = Double.parseDouble(data[3]);
} else if (data.length == 3) {
parkingFee = Double.parseDouble(data[2]);
}
if (productType.equals("R")) {
p = new Refreshment(productCode, name, cost);
} else if (productType.equals("P")) {
p = new ParkingPass(productCode, parkingFee);
}
productList.add(p);
}
scnr.close();
return productList;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
public static void jsonConverter(List<Product> products) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
File jsonOutput = new File("data/Products.json");
PrintWriter jsonPrintWriter = null;
try {
jsonPrintWriter = new PrintWriter(jsonOutput);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for (Product aProduct : products) {
String productOutput = gson.toJson(aProduct);
jsonPrintWriter.write(productOutput + "\n");
}
jsonPrintWriter.close();
}
public static void xmlConverter(List<Product> products) {
XStream xstream = new XStream();
File xmlOutput = new File("data/Products.xml");
PrintWriter xmlPrintWriter = null;
try {
xmlPrintWriter = new PrintWriter(xmlOutput);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
xmlPrintWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> \n <products>");
xstream.alias("product", Product.class);
xstream.alias("refreshment", Refreshment.class);
xstream.alias("parkingpass", ParkingPass.class);
for (Product aProduct: products) {
String productOutput = xstream.toXML(aProduct);
xmlPrintWriter.write( "\n" + productOutput);
}
xmlPrintWriter.write("\n");
xmlPrintWriter.write("</products>");
xmlPrintWriter.close();
}
public static void main(String[] args) {
List<Product> productList = readProduct();
jsonConverter(productList);
xmlConverter(productList);
}
}
}
XML outputs
JSON outputs
Related
I have an arraylist of object type:
public static ArrayList crimes = new ArrayList();
The CityCrime.java has the following:
public class CityCrime {
//Instance variables
private String city;
private String state;
private int population;
private int murder;
private int robbery;
private int assault;
private int burglary;
private int larceny;
private int motorTheft;
public int totalCrimes;
public static void main(String[] args) {
}
public int getTotalCrimes() {
return totalCrimes;
}
public void setTotalCrimes(int murder, int robbery, int assault, int burglary, int larceny, int motorTheft) {
this.totalCrimes = murder + robbery + assault + burglary + larceny + motorTheft;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
if(state.equalsIgnoreCase("ALABAMA")) {
this.state = "AL";
}
else if(state.equalsIgnoreCase("ALASKA")) {
this.state = "AK";
}
else if(state.equalsIgnoreCase("ARIZONA")) {
this.state = "AR";
}
//etc
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
public int getMurder() {
return murder;
}
public void setMurder(int murder) {
this.murder = murder;
}
public int getRobbery() {
return robbery;
}
public void setRobbery(int robbery) {
this.robbery = robbery;
}
public int getAssault() {
return assault;
}
public void setAssault(int assault) {
this.assault = assault;
}
public int getBurglary() {
return burglary;
}
public void setBurglary(int burglary) {
this.burglary = burglary;
}
public int getLarceny() {
return larceny;
}
public void setLarceny(int larceny) {
this.larceny = larceny;
}
public int getMotorTheft() {
return motorTheft;
}
public void setMotorTheft(int motorTheft) {
this.motorTheft = motorTheft;
}
public static void showAllMurderDetails() {
for (CityCrime crime : StartApp.crimes) {
System.out.println("Crime: City= " + crime.getCity() + ", Murder= " + crime.getMurder());
}
System.out.println();
}
public static int showAllViolentCrimes() {
int total = 0;
for(CityCrime crime : StartApp.crimes) {
total=total+crime.getMurder();
total=total+crime.getRobbery();
total=total+crime.getAssault();
}
System.out.println("Total of violent crimes: " + total);
return total;
}
public static int getPossessionCrimes() {
int total=0;
for (CityCrime crime : StartApp.crimes) {
total = total + crime.getBurglary();
total = total + crime.getLarceny();
total = total + crime.getMotorTheft();
}
System.out.println("Total of possession crimes: " + total);
return total;
}
}
The CityCrime ArrayList gets popular from another csv file:
public static void readCrimeData() {
File file = new File("crimeUSA.csv");
FileReader fileReader;
BufferedReader bufferedReader;
String crimeInfo;
String[] stats;
try {
fileReader = new FileReader(file);
bufferedReader = new BufferedReader(fileReader);
crimeInfo = bufferedReader.readLine();
crimeInfo = bufferedReader.readLine();
do {
CityCrime crime = new CityCrime(); // Default constructor
stats = crimeInfo.split(",");
{
if (stats[0] != null) {
crime.setCity(stats[0]);
}
if (stats[1] != null) {
crime.setState(stats[1]);
}
if (stats[2] != null) {
if (Integer.parseInt(stats[2]) >= 0) {
crime.setPopulation(Integer.parseInt(stats[2]));
}
}
if (stats[3] != null) {
if (Integer.parseInt(stats[3]) >= 0) {
crime.setMurder(Integer.parseInt(stats[3]));
}
}
if (stats[4] != null) {
if (Integer.parseInt(stats[4]) >= 0) {
crime.setRobbery(Integer.parseInt(stats[4]));
}
}
if (stats[5] != null) {
if (Integer.parseInt(stats[5]) >= 0) {
crime.setAssault(Integer.parseInt(stats[5]));
}
}
if (stats[6] != null) {
if (Integer.parseInt(stats[6]) >= 0) {
crime.setBurglary(Integer.parseInt(stats[6]));
}
}
if (stats[7] != null) {
if (Integer.parseInt(stats[7]) >= 0) {
crime.setLarceny(Integer.parseInt(stats[7]));
}
}
if (stats[8] != null) {
if (Integer.parseInt(stats[8]) >= 0) {
crime.setMotorTheft(Integer.parseInt(stats[8]));
}
}
crime.setTotalCrimes(Integer.parseInt(stats[3]), Integer.parseInt(stats[4]), Integer.parseInt(stats[5]), Integer.parseInt(stats[6]), Integer.parseInt(stats[7]), Integer.parseInt(stats[8]));
}
crimes.add(crime);
System.out.println(crime);
crimeInfo = bufferedReader.readLine();
} while (crimeInfo != null);
fileReader.close();
bufferedReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
I want to sort the array list and output to the csv file the list of robberies in descending order with the corresponding city. So far I have got the following, but I am stuck and not sure if going in the right direction. I'm relatively new to Java as you can probably tell so would appreciate it in as simple terms as can be:
public static void writeToFile() throws IOException {
File csvFile = new File("Robbery.csv");
FileWriter fileWriter = new FileWriter(csvFile);
ArrayList<Integer> robberyRates = new ArrayList();
for(CityCrime crime : crimes) {
robberyRates.add(crime.getRobbery());
}
Collections.sort(robberyRates);
}
The desired output will be for example:
Robbery,City
23511,New York
15863,Chicago
14353,Los Angeles
11371,Houston
10971,Philadelphia
etc…
Your help is appreciated. I have searched any page I can find on Google, but all I see from other example is writing to the csv from a set String ArrayList where they know the number of lines etc. Thankyou
You can try something like this:
public static void writeToFile() throws IOException {
File csvFile = new File("Robbery.csv");
try(FileWriter fileWriter = new FileWriter(csvFile)) { // try-with-resources to be sure that fileWriter will be closed at end
StringBuilder stringBuilder = new StringBuilder(); // Betther than String for multiple concatenation
crimes.sort(Comparator.comparingInt(CityCrime::getRobbery).reversed()); // I want to compare according to getRobbery, as Int, in reversed order
stringBuilder.append("Robbery")
.append(',')
.append("City")
.append(System.lineSeparator()); // To get new line character according to OS
for (final var crime : crimes)
stringBuilder.append(crime.getRobbery())
.append(',')
.append(crime.getCity())
.append(System.lineSeparator());
fileWriter.write(stringBuilder.toString());
}
}
If you are stuck because you can't figure out how to write a list, convert your list into a String and print the String instead.
I commented the piece of code, if you have any questions, don't hesitate.
Another solution is to use specific CSV parser library, OpenCSV for example.
It makes it easier to read and write CSV file.
Here a tutorial about OpenCSV:
https://mkyong.com/java/how-to-read-and-parse-csv-file-in-java/
I'm stuck trying to deserialize a list of Scores. I spent my entire day searching here but couldn't find a solution.. My code looks something like this:
public class Score implements Comparable<Score>, Serializable {
private String name;
private int score;
// .......
}
public class MySortedList<T> extends...implements...,Serializable {
private ArrayList<T> list;
// ....
}
public class ScoreManager {
private final String FILEPATH;
private final String FILENAME = "highscores.ser";
private MySortedList<Score> scoreList;
public ScoreManager() {
File workingFolder = new File("src\\games\\serialized");
if (!workingFolder.exists()) {
workingFolder.mkdir();
}
FILEPATH = workingFolder.getAbsolutePath();
if ((scoreList = loadScores()) == null) {
scoreList = new MySortedList<Score>();
}
}
public void addScore(Score score) {
scoreList.add(score);
saveScores();
}
public MySortedList<Score> getScoreList() {
return scoreList;
}
private void saveScores() {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File(FILEPATH, FILENAME)))) {
out.writeObject(scoreList);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
#SuppressWarnings("unchecked")
private MySortedList<Score> loadScores() {
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(FILEPATH, FILENAME)))) {
return (MySortedList<Score>) in.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
The loadScores() method returns just an empty MySortedList everytime.
However program succesfully creates the highscores.ser file in the correct place and I have absolutely no errors. Score objects are added correctly to the MySortedList object.
Any ideas? Perhaps worth mentioning that this is a part of a bigger program made in Swing. the methods in the ScoreManager class is called when the player dies
only if it can help, this code is working for me:
class Score implements Comparable<Score>, Serializable{
private int point;
public Score(int point) {
this.point = point;
}
public int getPoint(){
return point;
}
#Override
public int compareTo(Score o) {
if (o.getPoint() == this.getPoint())
return 0;
return this.point < o.getPoint() ? - 1 : 1;
}
public String toString() {
return "points: " + point;
}
}
class MyList<T> implements Serializable {
private ArrayList<T> list = new ArrayList<>();
public void add(T e){
list.add(e);
}
public void show() {
System.out.println(list);
}
}
public class Main {
File workingFolder;
String FILEPATH;
private final String FILENAME = "highscores.ser";
MyList<Score> list = new MyList<>();
public static void main(String[] args) {
Main main = new Main();
main.createFolder();
main.addItems();
main.saveScores();
MyList<Score> tempList = main.loadScores();
tempList.show();
main.addMoreItems();
main.saveScores();
tempList = main.loadScores();
tempList.show();
}
private void addItems() {
Score sc = new Score(10);
list.add(sc);
}
private void addMoreItems() {
Score sc1 = new Score(20);
list.add(sc1);
Score sc2 = new Score(30);
list.add(sc2);
}
private void createFolder() {
workingFolder = new File("src\\games\\serialized");
if (!workingFolder.exists()) {
workingFolder.mkdir();
}
FILEPATH = workingFolder.getAbsolutePath();
}
private void saveScores() {
System.out.println("before save: " + list);
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File(FILEPATH, FILENAME)))) {
out.writeObject(list);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
#SuppressWarnings("unchecked")
private MyList<Score> loadScores() {
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(FILEPATH, FILENAME)))) {
return (MyList<Score>) in.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
I am trying to read from a text file of the type (Artist, Title, Genre, Recordcompany, Release year, Number of songs, Playtime):
The Beatles, Abbey Road, Rock, Apple Records, 1969, 17, 47.16
Sia, 1000 Forms of Fear, Pop, Intertia, 2014, 12, 48.41
Taylor Swift, Speak Now
I have created a CD class:
package q;
public class CD {
//
private String artist;
private String titel;
private String genre;
private String recordcompany;
private int year; //
private int songs; //
private double playtime; //
public CD() { //
}
public CD(String newArtist, String newTitel) {
artist = newArtist;
titel = newTitel;
}
public CD(String newArtist, String newTitel, String newGenre, String newRecordcompany, int newYear, int newSongs, double newPlaytime) {
artist = newArtist;
titel = newTitel;
genre = newGenre;
recordcompany = newRecordcompany;
year = newYear;
songs = newSongs;
playtime = newPlaytime;
}
public String getArtist() { //
return artist;
}
public String getTitel() {
return titel;
}
public String getGenre() {
return genre;
}
public String getRecordcompany() {
return recordcompany;
}
public int getYear() {
return year;
}
public int getsong() {
return songs;
}
public double getPlaytime() {
return playtime;
}
public void setArtist(String newArtist) { //
artist = newArtist;
}
public void setTitel(String newTitel) {
titel = newTitel;
}
public void setGenre(String newGenre) {
genre = newGenre;
}
public void setRecordcompany(String newRecordcompany) {
recordcompany = newRecordcompany;
}
public void setYear(int newYear) {
year = newYear;
}
public void setSongs(int newSongs) {
songs = newSongs;
}
public void setplaytime(double newPlaytime) {
playtime = newPlaytime;
}
#
Override public String toString() { //
return ("Artist " + artist + System.lineSeparator() + "Titel: " + titel + System.lineSeparator() + "Genre: " + genre + System.lineSeparator() + "Recordcompany: " + recordcompany + System.lineSeparator() + "Year: " + year + System.lineSeparator() + "Songs: " + songs + System.lineSeparator() + "Playtime: " + playtime + System.lineSeparator());
}
}
I am trying to read from the text file and then convert it into a arraylist. I know I have overcomplicated it by first conerting the text file to a string array and then converting it to an arraylist. I would like to use set and get methods when I create the array, if it is possible. I wonder if any of you have any tips for me for how I can make the code less complicated and include set and get methods if possible, thank you.
package q;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class Q {
public static void main(String[] args) throws FileNotFoundException {
String artist = "";
String titel = "";
String genre = "";
String recordcompany = "";
String year = "";
String songs = "";
String playtime = "";
try {
BufferedReader br = new BufferedReader(new FileReader("nej.txt"));
String line = null;
while ((line = br.readLine()) != null) {
String tmp[] = line.split(",");
artist = tmp[0];
titel = tmp[1];
if (tmp.length > 2) {
genre = tmp[2];
recordcompany = tmp[3];
year = (tmp[4]);
songs = (tmp[5]);
playtime = (tmp[6]);
} else {
genre = "";
recordcompany = "";
year = "";
songs = "";
playtime = "";
}
List < String > unsorted = Arrays.asList(tmp);
for (String e: unsorted) {
System.out.println(e);
}
}
br.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
EDITED
package q;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Q {
public static void main(String[] args) throws FileNotFoundException {
String artist = "";
String titel = "";
String genre = "";
String recordcompany = "";
int year = 0;
int songs = 0;
double playtime = 0.0;
try {
BufferedReader br = new BufferedReader(new FileReader("nej.txt"));
String line = null;
List < CD > cdsList = new ArrayList < > ();
while ((line = br.readLine()) != null) {
CD cd = new CD();
String tmp[] = line.split(",");
cd.setArtist(tmp[0]);
cd.setTitel(tmp[1]);
if (tmp.length > 2) {
cd.setGenre(tmp[2]);
cd.setRecordcompany(tmp[3]);
cd.setYear(Integer.parseInt(tmp[4].trim()));
cd.setSongs(Integer.parseInt(tmp[5].trim()));
cd.setplaytime(Double.parseDouble(tmp[6].trim()));
}
System.out.println(cdsList);
}
br.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
Something like below.
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("nej.txt"));
String line = null;
List < CD > cdsList = new ArrayList < > ();
while ((line = br.readLine()) != null) {
CD cd = new CD();
String tmp[] = line.split(",");
cd.setArtist(tmp[0]);
cd.setTitel(tmp[1]);
if (tmp.length > 2) {
cd.setGenre(tmp[2]);
cd.setRecordcompany(tmp[3]);
cd.setYear(Integer.parseInt(tmp[4].trim()));
cd.setSongs(Integer.parseInt(tmp[5].trim()));
cd.setplaytime(Double.parseDouble(tmp[6].trim()));
}
cdsList.add(cd);
}
System.out.println(cdsList);
br.close();
} catch (IOException e) {
System.out.println(e);
}
}
Is there a possibility to get the String of the page's information in the about section?
An Example: https://www.facebook.com/FacebookDevelopers
Here is the info: "Build, grow, and monetize your app with Facebook. https://developers.facebook.com/"
I found out that the Facebook graph api supports this by the Field about on a Page.
Thanks for help in advance!
Best regards,
Dominic
you can below snippet code for getting facebook page information in java :
private static final String FacebookURL_PAGES = "me/accounts?fields=access_token,category,id,perms,picture{url},can_post,is_published,cover,fan_count,is_verified,can_checkin,global_brand_page_name,link,country_page_likes,is_always_open,is_community_page,new_like_count,overall_star_rating,name";
public List<FacebookPageModel> getPages(String accessToken) throws FacebookException, JSONException {
JSONObject posts = getBatch(accessToken,FacebookURL_PAGES);
Gson g =new Gson();
System.out.println(posts);
JSONArray postsData = posts.getJSONArray("data");
System.out.println(g.toJson(postsData));
return getPageList(postsData);
}
private JSONObject getBatch(String accessToken, String url) throws FacebookException{
Gson g = new Gson();
Facebook facebook = getFacebook(accessToken);
BatchRequests<BatchRequest> batch = new BatchRequests<BatchRequest>();
batch.add(new BatchRequest(RequestMethod.GET, url));
List<BatchResponse> results = facebook.executeBatch(batch);
BatchResponse result2 = results.get(0);
System.out.println(g.toJson(result2.asJSONObject()));
return result2.asJSONObject();
}
private Facebook getFacebook(String accessToken){
if(accessToken == null){
System.out.println("Access Token null while request for Facebook Instance !");
return null;
}
Facebook facebook = new FacebookFactory().getInstance();
facebook.setOAuthAppId(appId, appSecret);
facebook.setOAuthPermissions("email,publish_stream, publish_actions");
facebook.setOAuthAccessToken(new AccessToken(accessToken, null));
return facebook;
}
private List<FacebookPageModel> getPageList(JSONArray pagesData) throws JSONException{
Gson g = new Gson();
List<FacebookPageModel> pages = new ArrayList<FacebookPageModel>();
SimpleDateFormat desiredFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = null;
for (int i = 0; i < pagesData.length(); i++) {
FacebookPageModel obj = new FacebookPageModel();
JSONObject page = pagesData.getJSONObject(i);
System.out.println(g.toJson(page));
try{
obj.setAccessToken(page.getString("access_token"));
}catch(Exception ee){
obj.setAccessToken(null);
}
try{
obj.setCategory(page.getString("category"));
}catch(Exception ee){
obj.setCategory(null);
}
try{
obj.setId(page.getString("id"));
}catch(Exception ee){
obj.setId(null);
}
try{
obj.setName(page.getString("name"));
}catch(Exception ee){
obj.setName(null);
}
try{
obj.setCanPost(page.getBoolean("can_post"));
}catch(Exception ee){
obj.setCanPost(false);
}
try{
JSONObject picture = page.getJSONObject("picture");
JSONObject pictureData = picture.getJSONObject("data");
obj.setPageProfilePic(pictureData.getString("url"));
}catch(Exception ee){
obj.setCanPost(false);
}
try{
obj.setPublished(page.getBoolean("is_published"));
}catch(Exception ee){
obj.setPublished(false);
}
try{
obj.setFanCount(page.getLong("fan_count"));
}catch(Exception ee){
obj.setFanCount(0L);
}
try{
obj.setVerified(page.getBoolean("is_verified"));
}catch(Exception ee){
obj.setVerified(false);
}
try{
obj.setCanCheckin(page.getBoolean("can_checkin"));
}catch(Exception ee){
obj.setCanCheckin(false);
}
try{
obj.setGlobalBranPageName(page.getString("global_brand_page_name"));
}catch(Exception ee){
obj.setGlobalBranPageName(null);
}
try{
obj.setPageLink(page.getString("link"));
}catch(Exception ee){
obj.setPageLink(null);
}
try{
obj.setNewLikeCount(page.getLong("new_like_count"));
}catch(Exception ee){
obj.setNewLikeCount(0L);
}
try{
obj.setOverallStarRating(page.getLong("overall_star_rating"));
}catch(Exception ee){
obj.setOverallStarRating(0L);
}
try{
obj.setOverallStarRating(page.getLong("overall_star_rating"));
}catch(Exception ee){
obj.setOverallStarRating(0L);
}
pages.add(obj);
}
System.out.println(g.toJson(pages));
return pages;
}
public class FacebookPageModel {
//access_token
private String accessToken;
//name
private String name;
//id
private String id;
//category
private String category;
// can_post
private boolean canPost;
private String pageProfilePic;
// is_published
private boolean isPublished;
// fan_count
private long fanCount;
// is_verified
private boolean isVerified;
// can_checkin
private boolean canCheckin;
// global_brand_page_name
private String globalBranPageName;
// link
private String pageLink;
// new_like_count
private long newLikeCount;
// overall_star_rating
private long overallStarRating;
public boolean isCanPost() {
return canPost;
}
public void setCanPost(boolean canPost) {
this.canPost = canPost;
}
public String getPageProfilePic() {
return pageProfilePic;
}
public void setPageProfilePic(String pageProfilePic) {
this.pageProfilePic = pageProfilePic;
}
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public boolean isPublished() {
return isPublished;
}
public void setPublished(boolean isPublished) {
this.isPublished = isPublished;
}
public long getFanCount() {
return fanCount;
}
public void setFanCount(long fanCount) {
this.fanCount = fanCount;
}
public boolean isVerified() {
return isVerified;
}
public void setVerified(boolean isVerified) {
this.isVerified = isVerified;
}
public boolean isCanCheckin() {
return canCheckin;
}
public void setCanCheckin(boolean canCheckin) {
this.canCheckin = canCheckin;
}
public String getGlobalBranPageName() {
return globalBranPageName;
}
public void setGlobalBranPageName(String globalBranPageName) {
this.globalBranPageName = globalBranPageName;
}
public String getPageLink() {
return pageLink;
}
public void setPageLink(String pageLink) {
this.pageLink = pageLink;
}
public long getNewLikeCount() {
return newLikeCount;
}
public void setNewLikeCount(long newLikeCount) {
this.newLikeCount = newLikeCount;
}
public long getOverallStarRating() {
return overallStarRating;
}
public void setOverallStarRating(long overallStarRating) {
this.overallStarRating = overallStarRating;
}
}
I'm trying to read a very heavy JSON (over than 6000 objects) and store them on a hash map to insert it into my database later.
But the problem is that I face with OOM and that's cause from my heavy JSON, however GSON library should rid me from this situation, but it is not !!!
Any ideas?
public Map<String,String> readJsonStream(InputStream in) throws IOException
{
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
Map<String,String> contentMap = new HashMap<String,String>();
Gson mGson = new Gson();
contentMap = mGson.fromJson(reader, contentMap.getClass());
reader.close();
return contentMap;
}
From my experience, yes you can use google GSON to stream JSON data this is an example how to do it :
APIModel result = new APIModel();
try {
HttpResponse response;
HttpClient myClient = new DefaultHttpClient();
HttpPost myConnection = new HttpPost(APIParam.API_001_PRESENT(
serial_id, api_key));
try {
response = myClient.execute(myConnection);
Reader streamReader = new InputStreamReader(response
.getEntity().getContent());
JsonReader reader = new JsonReader(streamReader);
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("result")) {
if (reader.nextString() == "NG") {
result.setResult(Util.API_001_RESULT_NG);
break;
}
} else if (name.equals("items")) {
result = readItemsArray(reader);
} else {
reader.skipValue(); // avoid some unhandle events
}
}
reader.endObject();
reader.close();
} catch (Exception e) {
e.printStackTrace();
result.setResult(Util.API_001_RESULT_NG);
}
} catch (Exception e) {
e.printStackTrace();
result.setResult(Util.API_001_RESULT_NG);
}
readItemsArray function :
// read items array
private APIModel readItemsArray(JsonReader reader) throws IOException {
APIModel result = new APIModel();
String item_name, file_name, data;
result.setResult(Util.API_001_RESULT_OK);
reader.beginArray();
while (reader.hasNext()) {
item_name = "";
file_name = "";
data = "";
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("name")) {
item_name = reader.nextString();
} else if (name.equals("file")) {
file_name = reader.nextString();
} else if (name.equals("data")) {
data = reader.nextString();
} else {
reader.skipValue();
}
}
reader.endObject();
result.populateModel("null", item_name, file_name, data);
}
reader.endArray();
return result;
}
API Model Class :
public class APIModel {
private int result;
private String error_title;
private String error_message;
private ArrayList<String> type;
private ArrayList<String> item_name;
private ArrayList<String> file_name;
private ArrayList<String> data;
public APIModel() {
result = -1;
error_title = "";
error_message = "";
setType(new ArrayList<String>());
setItem_name(new ArrayList<String>());
setFile_name(new ArrayList<String>());
setData(new ArrayList<String>());
}
public void populateModel(String type, String item_name, String file_name, String data) {
this.type.add(type);
this.item_name.add(item_name);
this.file_name.add(file_name);
this.data.add(data);
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
public String getError_title() {
return error_title;
}
public void setError_title(String error_title) {
this.error_title = error_title;
}
public String getError_message() {
return error_message;
}
public void setError_message(String error_message) {
this.error_message = error_message;
}
public ArrayList<String> getType() {
return type;
}
public void setType(ArrayList<String> type) {
this.type = type;
}
public ArrayList<String> getItem_name() {
return item_name;
}
public void setItem_name(ArrayList<String> item_name) {
this.item_name = item_name;
}
public ArrayList<String> getFile_name() {
return file_name;
}
public void setFile_name(ArrayList<String> file_name) {
this.file_name = file_name;
}
public ArrayList<String> getData() {
return data;
}
public void setData(ArrayList<String> data) {
this.data = data;
}
}
before I use the streaming API from google GSON I also got OOM error because the JSON data I got is very big data (many images and sounds in Base64 encoding) but with GSON streaming I can overcome that error because it reads the data per token not all at once. And for Jackson JSON library I think it also have streaming API and how to use it almost same with my implementation with google GSON. I hope my answer can help you and if you have another question about my answer feel free to ask in the comment :)