Android Open Source - homeless-nyc J S O N Parser






From Project

Back to project page homeless-nyc.

License

The source code is released under:

GNU General Public License

If you think the Android project homeless-nyc listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package io.rasmi.homelessnyc;
//from w  w  w . j  a  v a 2 s .co m
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONTokener;

import android.content.Context;
import android.util.Log;

public class JSONParser {
  private static final String TAG = "JSONParser";
  private Context mContext;
  
  public JSONParser(Context context) { 
    mContext = context;
  }
  
  public ArrayList<facility> loadFacilities(String type) throws IOException, JSONException {
    String resourceName = "raw/" + type;
    
    ArrayList<facility> facilities = new ArrayList<facility>();
    BufferedReader reader = null;
    
    try {
      InputStream in = 
          mContext.getResources().openRawResource(mContext.getResources()
              .getIdentifier(resourceName,"raw", mContext.getPackageName()));
      
      reader = new BufferedReader(new InputStreamReader(in));
      StringBuilder jsonString = new StringBuilder();
      String line = null;
      
      while ((line = reader.readLine()) != null) {
        jsonString.append(line);
      }
      
      JSONArray array = (JSONArray) new JSONTokener(jsonString.toString()).nextValue();
      for (int i = 0; i < array.length(); i++) {
        facilities.add(new facility(array.getJSONObject(i)));
      }
      Log.i(TAG, "Loaded " + array.length() + " objects from JSON.");
    } catch(Exception e) {
      Log.e(TAG, "Error loading facilities from JSON. " + e.getLocalizedMessage());
    } finally {
      if (reader != null) 
        reader.close();
    }
    
    return facilities;
  }
  
  public ArrayList<service> loadServices(String type) throws IOException, JSONException {
    String resourceName = "raw/services_" + type;
    
    ArrayList<service> services = new ArrayList<service>();
    BufferedReader reader = null;
    
    try {
      InputStream in = 
          mContext.getResources().openRawResource(mContext.getResources()
              .getIdentifier(resourceName,"raw", mContext.getPackageName()));
      
      reader = new BufferedReader(new InputStreamReader(in));
      StringBuilder jsonString = new StringBuilder();
      String line = null;
      
      while ((line = reader.readLine()) != null) {
        jsonString.append(line);
      }
      
      JSONArray array = (JSONArray) new JSONTokener(jsonString.toString()).nextValue();
      for (int i = 0; i < array.length(); i++) {
        services.add(new service(array.getJSONObject(i)));
      }
      Log.i(TAG, "Loaded " + array.length() + " objects from JSON.");
    } catch(Exception e) {
      Log.e(TAG, "Error loading services from JSON. " + e.getLocalizedMessage());
    } finally {
      if (reader != null) 
        reader.close();
    }
    
    return services;
  }
  
}




Java Source Code List

io.rasmi.homelessnyc.JSONParser.java
io.rasmi.homelessnyc.MainActivity.java
io.rasmi.homelessnyc.MapActivity.java
io.rasmi.homelessnyc.facilitiesManager.java
io.rasmi.homelessnyc.facilityInfo.java
io.rasmi.homelessnyc.facility.java
io.rasmi.homelessnyc.faq.java
io.rasmi.homelessnyc.reportActivity.java
io.rasmi.homelessnyc.serviceInfo.java
io.rasmi.homelessnyc.serviceList.java
io.rasmi.homelessnyc.service.java
io.rasmi.homelessnyc.servicesManager.java
io.rasmi.homelessnyc.webAction.java