Rasmus Rajje Josefsson Rasmus Rajje Josefsson - 6 months ago 29
JSON Question

Jackson, can it handle two nested random keys?

Is it possible that jackson to handle following JSON structure? Because I don't really know how to create my POJO classes. Been trying to use hashmaps and other ways to, but no result.

My JSON structure:

{
"waypoints" : {
"-KH9UAPH5NmLJExaUa5g" : {
"-KH9UAPH5NmLJExaS2s" : {
"latitude" : 111,
"longitude" : 111.1
}
},
"-KHB1VjqUdO90vxj9XCh" : {
"-KHB1VjqUdO90vxj9XCi" : {
"latitude" : 222.1,
"longitude" : 222.11
},
"-KHB1ZykbwgXM9sPNie9" : {
"latitude" : 222.2,
"longitude" : 222.22
}
}
}
},


UPDATE

The inner key should contain the following

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;


@JsonIgnoreProperties(ignoreUnknown = true)
public class MyWaypoint {

@JsonProperty("latitude")
private double latitude;
@JsonProperty("longitude")
private double longitude;


public MyWaypoint() {
}

public MyWaypoint(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}

public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
}


CODE UPDATE

Its inside the recyclerView in the populateViewHolder, that will loop for each item in the database. In my case I want it to be for each item (key) in the waypoints.

MyWaypoints
is the POJO entry in this case.

private Firebase mRef;
private Firebase mUserRef;
private String mUserId;



FirebaseRecyclerAdapter<MyWaypoints, LatLngViewHolderBack2> mAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

mRef = new Firebase(Constants.FIREBASE_URL);
if (mRef.getAuth() == null) {
loadLoginView();
}


try {
mUserId = mRef.getAuth().getUid();
} catch (Exception e) {
loadLoginView();
}


final RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
mRecyclerView.setHasFixedSize(false);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setReverseLayout(false);
mRecyclerView.setLayoutManager(manager);


// https://todoapprj.firebaseio.com/users/1a96a633-7e67-41b8-9aa7-c70d4b7eb59c
final String userUrl = Constants.FIREBASE_URL + "/users/" + mUserId + "/waypoints";
mUserRef = new Firebase(userUrl);


mAdapter = new FirebaseRecyclerAdapter<MyWaypoints, LatLngViewHolderBack2>(MyWayoints.class, R.layout.list_item, LatLngViewHolderBack2.class, mUserRef) {
@Override
protected void populateViewHolder(LatLngViewHolderBack2 latLngViewHolder, MyWaypoints item, int i) {



// This will only acces the first key, where there is no latitudes or longitudes.
// String lon = item.getLongitude() + "";
// latLngViewHolder.locationA.setText(lon);

// Random KEY1
// ---> I'M HERE NOW <---
// Random KEY2
// ***Want to be here<***


/********** Is it here i should put the JsonParse? **********/


}
};

mRecyclerView.setAdapter(mAdapter);

}


Current Log

This is what I'm getting wrong:

05-19 07:31:45.515 11345-11399/com.example.rasmusjosefsson.rjcar I/OpenGLRenderer: Initialized EGL, version 1.4
05-19 07:31:49.281 11345-11345/com.example.rasmusjosefsson.rjcar I/dynamic keys: -KH9UAPH5NmLJExaUa5g
05-19 07:31:49.281 11345-11345/com.example.rasmusjosefsson.rjcar I/LOG LONG: 111.1
05-19 07:31:49.281 11345-11345/com.example.rasmusjosefsson.rjcar I/dynamic keys: -KHB1VjqUdO90vxj9XCh
05-19 07:31:49.282 11345-11345/com.example.rasmusjosefsson.rjcar I/LOG LONG: 222.11
05-19 07:31:49.282 11345-11345/com.example.rasmusjosefsson.rjcar I/LOG LONG: 222.22

Answer

So after clarification (within the comments) - please find my updated answer.

The crux of the solution lays in the use of the keys() iterator to iterate over all the properties.

private void parseJson() throws JSONException {
    String data = "{\n" +
            "      \"users\" : {" +
            "        \"0057242b-81e2-4f97-bca7-b671212614ba\" : {" +
            "          \"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" +
            "              }" +
            "            }" +
            "          }" +
            "        }}}";

    JSONObject response = new JSONObject(data);
    JSONObject users = response.getJSONObject("users");
    JSONObject guid = users.getJSONObject("0057242b-81e2-4f97-bca7-b671212614ba");
    JSONObject waypoint = guid.getJSONObject("waypoints");

    Iterator keys = waypoint.keys();
    while(keys.hasNext()) {
        // loop to get the dynamic keys
        String currentDynamicKey = (String)keys.next();
        Log.i("dynamic keys", currentDynamicKey);
        JSONObject obj = waypoint.getJSONObject(currentDynamicKey);
        Iterator key = obj.keys();
        while(key.hasNext()) {
            // loop to get the dynamic keys
            String currentDynamicKey2 = (String)key.next();
            JSONObject longlat = obj.getJSONObject(currentDynamicKey2);
            String lat = longlat.getString("latitude");
            String lng = longlat.getString("longitude");
        }
    }
}

My logcat output for the above JSON:

05-19 15:39:37.676 6324-6324/ronin.fansboy I/dynamic keys: -KH9UAPH5NmLJExaUa5g 
05-19 15:39:37.676 6324-6324/ronin.fansboy I/lat: 111 
05-19 15:39:37.687 6324-6324/ronin.fansboy I/lng: 111.1 
05-19 15:39:37.697 6324-6324/ronin.fansboy I/dynamic keys: -KHB1VjqUdO90vxj9XCh 
05-19 15:39:37.697 6324-6324/ronin.fansboy I/lat: 222.1 
05-19 15:39:37.707 6324-6324/ronin.fansboy I/lng: 222.11 
05-19 15:39:37.717 6324-6324/ronin.fansboy I/lat: 222.2 
05-19 15:39:37.727 6324-6324/ronin.fansboy I/lng: 222.22