How to transform a nested list of double values into a Java class using RxJava?

后端 未结 3 1276
灰色年华
灰色年华 2021-01-27 00:18

In my Android client I receive this JSON data from a backend:

[
    [
        1427378400000,
        553
    ],
    [
        1427382000000,
        553
    ]
]
         


        
3条回答
  •  不知归路
    2021-01-27 01:06

    According to your Data, you receive a list of pair (timestamp, level). This pair is represented by a list which contains only 2 values.

    So you want to emit each pair, and transform each pair into a ProductLevel.

    To do this, you'll have to flatMap your list of pair to emit each pair. Then to map each pair into a ProductLevel. Finally, just build a list with all emited items.

    (java8 style)

    AppObservable.bindFragment(this, responseObservable)
                 .subscribeOn(Schedulers.io())
                 .observeOn(AndroidSchedulers.mainThread())
                 .flatMapIterable(listOfList -> listOfList) // or flatMap(l -> Observable.from(l))
                 .map(pair -> new ProductLevel(pair.get(0),pair.get(1))) // build ProductLevel for each pair
                 .toList() // build a list with all ProductLevel
                 .subscribe(listOfProductLevel -> /** ... **/);
    

提交回复
热议问题