Android Open Source - RateBeer-Mobile R B J S O N Parser






From Project

Back to project page RateBeer-Mobile.

License

The source code is released under:

GNU General Public License

If you think the Android project RateBeer-Mobile 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

/*
 * Copyright 2010, Jesper Fussing Mrk/*from  w w w  .ja v a  2s.  c  o  m*/
 *
 * This file is part of Ratebeer Mobile for Android.
 *
 * Ratebeer Mobile 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.
 *
 * Ratebeer Mobile 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 Ratebeer Mobile.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.moerks.ratebeermobile.util;

import java.util.ArrayList;
import java.util.List;

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

import android.util.Log;
import dk.moerks.ratebeermobile.exceptions.RBParserException;
import dk.moerks.ratebeermobile.vo.BeerInfo;
import dk.moerks.ratebeermobile.vo.PlacesInfo;
import dk.moerks.ratebeermobile.vo.Review;
import dk.moerks.ratebeermobile.vo.SearchResult;

public class RBJSONParser {
  private static final String LOGTAG = "RBJSONParser";
  
  public static List<PlacesInfo> parsePlaces(String responseString) throws RBParserException {
    ArrayList<PlacesInfo> results = new ArrayList<PlacesInfo>();

    try {
      Log.d(LOGTAG, "Creating JSON Object");
      JSONArray jsonObjects = new JSONArray(responseString);
      
      Log.d(LOGTAG, "ARRAY LENGTH: " + jsonObjects.length());
      for (int i = 0; i < jsonObjects.length(); i++) {
        PlacesInfo place = new PlacesInfo();
        
        Log.d(LOGTAG, "JSONObject("+i+"): " + jsonObjects.get(i));
        JSONObject json = new JSONObject(jsonObjects.getString(i));
        
        //{"PlaceID":6996,"PlaceName":"Lush Wine and Spirits","PlaceType":"3","Address":"1257 S. Halsted St.",
        //"City":"Chicago","StateID":14,"CountryID":213,"PostalCode":"60607","PhoneNumber":"738-1900","AvgRating":33.5,
        //"PhoneAC":"312","Latitude":41.865794,"Longitude":-87.646683,"Distance":1.368293}
        place.setPlaceID(json.getString("PlaceID"));
        place.setPlaceName(json.getString("PlaceName"));
        place.setPlaceType(json.getString("PlaceType"));
        place.setAddress(json.getString("Address"));
        place.setCity(json.getString("City"));
        place.setStateID(json.getString("StateID"));
        place.setCountryID(json.getString("CountryID"));
        place.setPostalCode(json.getString("PostalCode"));
        place.setPhoneNumber(json.getString("PhoneNumber"));
        place.setAvgRating(json.getString("AvgRating"));
        place.setPhoneAC(json.getString("PhoneAC"));
        place.setLatitude(json.getString("Latitude"));
        place.setLongitude(json.getString("Longitude"));
        place.setDistance(json.getString("Distance"));
        
        results.add(place);
      }
      
      return results;
    } catch(JSONException e){
      throw new RBParserException(LOGTAG, "Unable to parse places", e);
    }
  }
  
  public static List<SearchResult> parseSearch(String responseString) throws RBParserException {
    ArrayList<SearchResult> results = new ArrayList<SearchResult>();
    
    try {
      Log.d(LOGTAG, "Creating JSON Object");
      JSONArray jsonObjects = new JSONArray(responseString);
      
      Log.d(LOGTAG, "ARRAY LENGTH: " + jsonObjects.length());
      for (int i = 0; i < jsonObjects.length(); i++) {
        SearchResult searchResult = new SearchResult();
        
        //"BeerID":106264,"BeerName":"Skands 5 \u00C5r","BrewerID":4147,"OverallPctl":52,"RateCount":17,"IsAlias":false,"Retired":false,"State":" ","Country":"Denmark ","IsRated":0
        Log.d(LOGTAG, "JSONObject("+i+"): " + jsonObjects.get(i));
        JSONObject json = new JSONObject(jsonObjects.getString(i));
        
        searchResult.setBeerId(json.getString("BeerID"));
        searchResult.setBeerName(json.getString("BeerName"));
        searchResult.setBeerPercentile(json.getString("OverallPctl"));
        searchResult.setBeerRatings(json.getString("RateCount"));
        searchResult.setRated(json.getString("IsRated").equals("1"));
        searchResult.setAlias(json.getBoolean("IsAlias"));
        searchResult.setRetired(json.getBoolean("Retired"));
        
        results.add(searchResult);
      }
    } catch(JSONException e){
      throw new RBParserException(LOGTAG, "Unable to parse search", e);
    }
    
    return results;
  }

  public static List<Review> parseReviews(String responseString) throws RBParserException {
    ArrayList<Review> results = new ArrayList<Review>();

    try {
      Log.d(LOGTAG, "Creating JSON Object");
      JSONArray jsonObjects = new JSONArray(responseString);
      
      Log.d(LOGTAG, "ARRAY LENGTH: " + jsonObjects.length());
      for (int i = 0; i < jsonObjects.length(); i++) {
        Review review = new Review();
        
        Log.d(LOGTAG, "JSONObject("+i+"): " + jsonObjects.get(i));
        JSONObject json = new JSONObject(jsonObjects.getString(i));
        
        review.setResultNum(json.getString("resultNum"));
        review.setRatingId(json.getString("RatingID"));
        review.setAppearance(json.getString("Appearance"));
        review.setAroma(json.getString("Aroma"));
        review.setComments(json.getString("Comments"));
        review.setFlavor(json.getString("Flavor"));
        review.setMouthfeel(json.getString("Mouthfeel"));
        review.setOverall(json.getString("Overall"));
        review.setTotalScore(json.getString("TotalScore"));
        review.setTimeEntered(json.getString("TimeEntered"));
        review.setTimeUpdated(json.getString("TimeUpdated"));
        review.setUserId(json.getString("UserID"));
        review.setUserName(json.getString("UserName"));
        review.setCity(json.getString("City"));
        review.setStateId(json.getString("StateID"));
        review.setState(json.getString("State"));
        review.setCountryId(json.getString("CountryID"));
        review.setCountry(json.getString("Country"));
        review.setRateCount(json.getString("RateCount"));
        
        results.add(review);
      }    
    } catch(JSONException e){
      throw new RBParserException(LOGTAG, "Unable to parse beer reviews", e);
    }
    
    return results;
  }

  public static BeerInfo parseBeerInfo(String responseString) throws RBParserException {
    BeerInfo info = new BeerInfo();
    
    try {
      Log.d(LOGTAG, "Creating JSON Object");
      JSONArray jsonObjects = new JSONArray(responseString);

      Log.d(LOGTAG, "JSONObject(0): " + jsonObjects.get(0));
      JSONObject json = jsonObjects.getJSONObject(0);
      
      info.setBeerId(json.getString("BeerID"));
      info.setBeerName(json.getString("BeerName"));
      info.setBrewerId(json.getString("BrewerID"));
      info.setBrewerName(json.getString("BrewerName"));
      info.setOverallPctl(json.getLong("OverallPctl"));
      info.setBeerStyleName(json.getString("BeerStyleName"));
      info.setDescription(json.getString("Description"));
      info.setAbv(json.getString("Alcohol"));
    } catch(JSONException e){
      throw new RBParserException(LOGTAG, "Unable to parse beer info", e);
    }
    
    return info;
  }

  public static Integer parseRateCount(String responseString) throws RBParserException {
    int count = 0;
    try {
      Log.d(LOGTAG, "Creating JSON Object");
      JSONArray jsonObjects = new JSONArray(responseString);

      Log.d(LOGTAG, "JSONObject(0): " + jsonObjects.get(0));
      JSONObject json = jsonObjects.getJSONObject(0);

      count = json.getInt("RateCount");
    } catch(JSONException e){
      throw new RBParserException(LOGTAG, "Unable to parse rate count", e);
    }
    
    return new Integer(count);
  }
}




Java Source Code List

com.github.droidfu.DroidFuApplication.java
com.github.droidfu.DroidFu.java
com.github.droidfu.activities.BetterActivityHelper.java
com.github.droidfu.activities.BetterActivity.java
com.github.droidfu.activities.BetterDefaultActivity.java
com.github.droidfu.activities.BetterListActivity.java
com.github.droidfu.adapters.ListAdapterWithProgress.java
com.github.droidfu.concurrent.BetterAsyncTaskCallable.java
com.github.droidfu.concurrent.BetterAsyncTask.java
com.github.droidfu.services.BetterService.java
dk.moerks.ratebeermobile.BeerMail.java
dk.moerks.ratebeermobile.BeerView.java
dk.moerks.ratebeermobile.Home.java
dk.moerks.ratebeermobile.MailAction.java
dk.moerks.ratebeermobile.MailView.java
dk.moerks.ratebeermobile.PlaceView.java
dk.moerks.ratebeermobile.Places.java
dk.moerks.ratebeermobile.RateBeer.java
dk.moerks.ratebeermobile.Rate.java
dk.moerks.ratebeermobile.Rating.java
dk.moerks.ratebeermobile.Search.java
dk.moerks.ratebeermobile.Settings.java
dk.moerks.ratebeermobile.activity.BetterRBActivity.java
dk.moerks.ratebeermobile.activity.BetterRBDefaultActivity.java
dk.moerks.ratebeermobile.activity.BetterRBListActivity.java
dk.moerks.ratebeermobile.activity.RBActivity.java
dk.moerks.ratebeermobile.adapters.FeedAdapter.java
dk.moerks.ratebeermobile.adapters.MessageAdapter.java
dk.moerks.ratebeermobile.adapters.PlacesAdapter.java
dk.moerks.ratebeermobile.adapters.ReviewAdapter.java
dk.moerks.ratebeermobile.adapters.SearchAdapter.java
dk.moerks.ratebeermobile.exceptions.LocationException.java
dk.moerks.ratebeermobile.exceptions.LoginException.java
dk.moerks.ratebeermobile.exceptions.NetworkException.java
dk.moerks.ratebeermobile.exceptions.RBException.java
dk.moerks.ratebeermobile.exceptions.RBParserException.java
dk.moerks.ratebeermobile.io.NetBroker.java
dk.moerks.ratebeermobile.io.TwitterPoster.java
dk.moerks.ratebeermobile.overlays.PinOverlayItem.java
dk.moerks.ratebeermobile.receivers.BeerMailServiceReceiver.java
dk.moerks.ratebeermobile.receivers.BootReceiver.java
dk.moerks.ratebeermobile.services.BeerMailService.java
dk.moerks.ratebeermobile.services.BeerSearchHistoryProvider.java
dk.moerks.ratebeermobile.task.BarcodeLookupTask.java
dk.moerks.ratebeermobile.task.BetterRBTask.java
dk.moerks.ratebeermobile.task.DeleteBeermailTask.java
dk.moerks.ratebeermobile.task.PostTwitterStatusTask.java
dk.moerks.ratebeermobile.task.RefreshFriendFeedTask.java
dk.moerks.ratebeermobile.task.RetrieveBeerImageTask.java
dk.moerks.ratebeermobile.task.RetrieveBeerInfoTask.java
dk.moerks.ratebeermobile.task.RetrieveBeerReviewsTask.java
dk.moerks.ratebeermobile.task.RetrieveBeermailTask.java
dk.moerks.ratebeermobile.task.RetrieveBeermailsTask.java
dk.moerks.ratebeermobile.task.RetrievePlacesTask.java
dk.moerks.ratebeermobile.task.RetrieveRatingTask.java
dk.moerks.ratebeermobile.task.RetrieveUserIdTask.java
dk.moerks.ratebeermobile.task.SaveRatingTask.java
dk.moerks.ratebeermobile.task.SearchTask.java
dk.moerks.ratebeermobile.task.SendBeermailTask.java
dk.moerks.ratebeermobile.task.SetDrinkingBeerIdTask.java
dk.moerks.ratebeermobile.task.SetDrinkingStatusTask.java
dk.moerks.ratebeermobile.util.BCPParser.java
dk.moerks.ratebeermobile.util.RBJSONParser.java
dk.moerks.ratebeermobile.util.RBParser.java
dk.moerks.ratebeermobile.util.StringUtils.java
dk.moerks.ratebeermobile.vo.BeerInfo.java
dk.moerks.ratebeermobile.vo.Feed.java
dk.moerks.ratebeermobile.vo.MessageHeader.java
dk.moerks.ratebeermobile.vo.Message.java
dk.moerks.ratebeermobile.vo.PlacesInfo.java
dk.moerks.ratebeermobile.vo.RatingData.java
dk.moerks.ratebeermobile.vo.Review.java
dk.moerks.ratebeermobile.vo.SearchResult.java