com.jjoseba.podemosquotes.model.JSONReader.java Source code

Java tutorial

Introduction

Here is the source code for com.jjoseba.podemosquotes.model.JSONReader.java

Source

/*
This file is part of PodemosQuotes.
    
PodemosQuotes is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
PodemosQuotes is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with PodemosQuotes.  If not, see <http://www.gnu.org/licenses/>
    
 */

package com.jjoseba.podemosquotes.model;

import android.content.Context;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

public class JSONReader {

    public static final String TAG = JSONReader.class.getName();
    public static final String JSON_NAME = "quotes.json";

    private Context ctx;

    public JSONReader(Context ctx) {
        this.ctx = ctx;
    }

    private String readJSONFile() {

        StringBuilder sb = new StringBuilder();
        try {
            InputStream input = ctx.getAssets().open(JSON_NAME);
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return sb.toString();

    }

    public ArrayList<Quote> parse() {
        String contentsJson = readJSONFile();
        HashMap<String, QuoteAuthor> authors = new HashMap<>();
        ArrayList<Quote> quotes = new ArrayList<>();

        try {
            JSONObject json = new JSONObject(contentsJson);
            JSONArray authorsArray = json.getJSONArray("authors");
            for (int i = 0; i < authorsArray.length(); ++i) {
                JSONObject auth = (JSONObject) authorsArray.get(i);
                QuoteAuthor author = new QuoteAuthor();
                author.setName(auth.getString("name"));
                author.setImagePath(auth.getString("image"));
                author.setPosition(auth.getString("position"));

                authors.put(auth.getString("id"), author);
            }

            JSONArray quotesArray = json.getJSONArray("quotes");
            for (int i = 0; i < quotesArray.length(); ++i) {
                JSONObject q = (JSONObject) quotesArray.get(i);
                Quote quote = new Quote();
                quote.setQuote(q.getString("quote"));
                quote.setSoundPath(q.getString("sound"));
                quote.setAuthor(authors.get(q.getString("author")));
                quotes.add(quote);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return quotes;
    }

}