要遍历数组列表,可以使用以下代码
List<LatLng> latLngs = new ArrayList<>();
latLngs.add(new LatLng(5,5));
Observable.just(latLngs)
.concatMap(array -> Observable.fromIterable(array))
.subscribe(v-> Log.d(TAG, "subscribeToTopic: "+v.latitude));
但是如何使用Rxjava这样遍历JSONarray?
[{
"vehicleId": "5c11e863a361762fdbe99fae",
"latitude": "19.952829",
"latitudeDir": "N",
"longitude": "73.735054",
"longitudeDir": "E",
"speed": "0.0",
"ignition": "0",
"batteryStatus": "1"
}, {
"vehicleId": "5c11e863a361762fdbe99fae",
"latitude": "19.952829",
"latitudeDir": "N",
"longitude": "73.735054",
"longitudeDir": "E",
"speed": "0.0",
"ignition": "0",
"batteryStatus": "1"
}, {
"vehicleId": "5c11e863a361762fdbe99fae",
"latitude": "19.952829",
"latitudeDir": "N",
"longitude": "73.735054",
"longitudeDir": "E",
"speed": "0.0",
"ignition": "0",
"batteryStatus": "1"
}, {
"vehicleId": "5c11e863a361762fdbe99fae",
"latitude": "19.952829",
"latitudeDir": "N",
"longitude": "73.735054",
"longitudeDir": "E",
"speed": "0.0",
"ignition": "0",
"batteryStatus": "1"
}]
答案 0 :(得分:1)
由于JSONArray实现了List接口(假设您在此处使用了simplejson),因此您可以执行以下操作:
JSONArray jsonArray = new JSONArray();
Observable.fromIterable(jsonArray)
.subscribe(e -> {
System.out.println((JSONObject) e);
});
编辑:根据您的评论,我认为您没有使用simplejson,但是此org.json.JSONArray并未实现List接口。在这种情况下,您可以像这样进行迭代:
JSONArray jsonArray = new JSONArray();
Observable.range(0,jsonArray.length())
.map(jsonArray::get)
.subscribe(e -> {
System.out.println((JSONObject) e);
});