【发布时间】:2016-05-19 10:31:17
【问题描述】:
杰克逊有可能处理以下 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
【问题讨论】: