Skip to content

Instantly share code, notes, and snippets.

@ishmaelmakitla
Created May 9, 2016 20:53
Show Gist options
  • Save ishmaelmakitla/2e886fa5b1d46aa82cf10e838f2a25b5 to your computer and use it in GitHub Desktop.
Save ishmaelmakitla/2e886fa5b1d46aa82cf10e838f2a25b5 to your computer and use it in GitHub Desktop.
/**
This object represents JSON string:
{
"email" : "kalle@hotmail.se",
"waypoints" : {
"-KH9UAPH5NmLJExaUa5g" : {
"-KH9UAPH5NmLJExaS2s" : {
"latitude" : 111,
"longitude" : 111.1
}
},
"-KHB1VjqUdO90vxj9XCh" : {
"-KHB1VjqUdO90vxj9XCi" : {
"latitude" : 222.1,
"longitude" : 222.11
},
"-KHB1ZykbwgXM9sPNie9" : {
"latitude" : 222.2,
"longitude" : 222.22
}
}
}
}
*/
public class User{
String email;
Object waypoints;
public User(){}
public String getEmail(){return this.email; }
public void setEmail(String _email){this.email = _email;}
public Object getWaypoints(){return waypoints'}
public void setWaypoints(Object points){ this.waypoints = points;}
public Map<String, MyWaypoint> getMyWaypoints(){
if(waypoints == null || waypoint.toString().isEmpty()){ return null; }
JSONObject waypointJSON = new JSONObject(waypoint.toString());
//convert to Map
Map<String, MyWaypoint> map = new HashMap<String, MyWaypoint>();
Iterator<String> keysIterator = waypointJSON.keys();
while(keysIterator.hasNext()) {
String key = keysIterator.next();
Object value = object.get(key);
//the value is JSONObject - something like
/*
"-KH9UAPH5NmLJExaS2s" : {"latitude" : 111,"longitude" : 111.1}, {}, {}
So to iterate through all these
*/
if(value instanceof JSONObject) {
valueJSONObject = (JSONObject)value;
Iterator<String> valueKeysIterator = valueJSONObject.keys();
while(valueKeysIterator.hasNext()){
//the value of each key is the MyWaypoint object
String valueKey = valueKeysIterator.next();
JSONObject myWayPointJsonValue = (JSONObject)object.get(valueKey);
MyWaypoint myWayPointInstance = new MyWaypoint(myWayPointJsonValue.get("latitute"), myWayPointJsonValue.get("longitude"));
map.put(valueKey, myWayPointInstance);
}
}
}
return map;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment