杰克逊有可能处理以下JSON结构吗?因为我真的不知道如何创建我的POJO类。一直试图使用hashmaps和其他方法,但没有结果。
我的JSON结构:
{
"waypoints" : {
"-KH9UAPH5NmLJExaUa5g" : {
"-KH9UAPH5NmLJExaS2s" : {
"latitude" : 111,
"longitude" : 111.1
}
},
"-KHB1VjqUdO90vxj9XCh" : {
"-KHB1VjqUdO90vxj9XCi" : {
"latitude" : 222.1,
"longitude" : 222.11
},
"-KHB1ZykbwgXM9sPNie9" : {
"latitude" : 222.2,
"longitude" : 222.22
}
}
}
},
更新
内键应包含以下内容
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;
}
}
代码更新
它位于populateViewHolder中的recyclerView内部,它将循环访问数据库中的每个项目。在我的情况下,我希望它适用于航点中的每个项目(关键)。
在这种情况下, MyWaypoints
是POJO条目。
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);
}
当前日志
这就是我错了:
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
答案 0 :(得分:0)
您可以使用此link 从json创建POJO。可以选择设置其他属性(它是一个列表)。
答案 1 :(得分:0)
所以在澄清之后(在评论中) - 请找到我更新的答案。
解决方案的关键在于使用keys()迭代器迭代所有属性。
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");
}
}
}
以上JSON的我的logcat输出:
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