Mechele

Untitled

Feb 6th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. package com.taxipixi.entity.json;
  2.  
  3. import com.taxipixi.entity.MapCoordinates;
  4. import com.taxipixi.entity.Place;
  5. import org.json.JSONException;
  6. import org.json.JSONObject;
  7.  
  8. public class PlaceJsonParser implements JsonParser<Place> {
  9.  
  10. public static final String ID = "id";
  11. public static final String ADDRESS = "address";
  12. public static final String LATITUDE = "latitude";
  13. public static final String LONGITUDE = "longitude";
  14.  
  15. public Place parse(JSONObject object) throws JSONException {
  16. Place place = null ;
  17.  
  18. if(!object.isNull(LATITUDE) && !object.isNull(LONGITUDE)) {
  19. place = new Place();
  20.  
  21. place.setId(object.getLong(ID));
  22. place.setDescription(object.getString(ADDRESS));
  23.  
  24. MapCoordinates coords = new MapCoordinates();
  25. //MK: lat lng getting rounded off to integer
  26. coords.setLatitude(object.getDouble(LATITUDE));
  27.  
  28. coords.setLongitude(object.getDouble(LONGITUDE));
  29.  
  30. place.setMapCoordinates(coords);
  31. }
  32. return place;
  33. }
  34.  
  35. public JSONObject asJson(Place place) throws JSONException {
  36. JSONObject jsonObject = new JSONObject();
  37.  
  38. jsonObject.put(ADDRESS, place.getDescription());
  39. jsonObject.put(LATITUDE, place.getLatitude());
  40. jsonObject.put(LONGITUDE, place.getLongitude());
  41.  
  42. return jsonObject;
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment