Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gson解析 #50

Closed
xiaobailong24 opened this issue Apr 3, 2017 · 2 comments
Closed

Gson解析 #50

xiaobailong24 opened this issue Apr 3, 2017 · 2 comments

Comments

@xiaobailong24
Copy link
Collaborator

xiaobailong24 commented Apr 3, 2017

您好,
关于json解析,请教一个问题,形如以下格式的json应该怎么解析?元素的key是变化的。万分感谢!

{
    "127": {
        "OSID": "127",
        "name": "CentOS 6 x64",
        "arch": "x64",
        "family": "centos",
        "windows": false
    },
    "148": {
        "OSID": "148",
        "name": "Ubuntu 12.04 i386",
        "arch": "i386",
        "family": "ubuntu",
        "windows": false
    }
}
@xiaobailong24
Copy link
Collaborator Author

xiaobailong24 commented Apr 3, 2017

1、在API里定义接口返回Map对象:

Observable<Map<String, Bean>> getBeans();

2、在Contract里定义同样的接口

Observable<Map<String, Bean>> getBeans();

3、在Model中实现该接口

Observable<Map<String, Bean>> getBeans(){
    return mServiceManager.getBeanService().getBeans();
}

4、在Presenter中请求网络,获取数据

    public void requestBeans() {
        mModel.getBeans()
                .xxxxxx
                .flatMap(beanMap -> {
                    mBeanList.clear();
                    try {
                        for (String key : beanMap .keySet()) {
                            //获得所有value
                            mBeanList.add(beanMap .get(key));
                        }
                        return Observable.fromIterable(mBeanList);
                    } catch (Exception e) {
                        Timber.tag(TAG).e(e.getMessage());
                    }
                    return null;
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(serverMachine ->
                                mAdapter.notifyDataSetChanged(),
                        throwable -> {
                            Timber.tag(TAG).e(throwable.getMessage());
                            UiUtils.SnackbarText("net error");
                        });
    }

@JessYanCoding
Copy link
Owner

Well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants