JSON構造を次のように扱うことはできますか? POJOクラスの作成方法はわからないからです。ハッシュマップやその他の方法を使用しようとしましたが、結果はありません。ジャクソン、2つのネストされたランダムキーを処理できますか?
私のJSONの構造:
{
"waypoints" : {
"-KH9UAPH5NmLJExaUa5g" : {
"-KH9UAPH5NmLJExaS2s" : {
"latitude" : 111,
"longitude" : 111.1
}
},
"-KHB1VjqUdO90vxj9XCh" : {
"-KHB1VjqUdO90vxj9XCi" : {
"latitude" : 222.1,
"longitude" : 222.11
},
"-KHB1ZykbwgXM9sPNie9" : {
"latitude" : 222.2,
"longitude" : 222.22
}
}
}
},
UPDATE
内側のキーは、次の
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;
}
}
コード更新
が含まれている必要があります0populateViewHolder内の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 + "https://stackoverflow.com/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
はい、私は知っているが、それはああ、私が見 –
追加されます新しいランダムキーを私は事前に知っているキーのためにのみ発生し、そしてません。あなたはウェイポイントが動的に追加され、あらかじめキーの名前がわからないことを意味しますか? –
はい、これには良い答えがありますか? –