我正在寻找如何在我的 angular2 应用程序中使用 JSON 的最佳解决方案。
我的 JSON 是:
{
"rightUpperLogoId": {
"id": 100000,
"value": ""
},
"navbarBackgroundColorIdCss": {
"id": 100001,
"value": ""
},
"backgroundColorIdCss": {
"id": 100002,
"value": ""
},
"translationIdFrom": {
"value": "90000"
},
"translationIdTo": {
"value": "90055"
}
}
这个 JSON 类似于应用程序 UI 的配置文件。在我的应用程序中,我想从 rightUpperLogoId 获取 id,它是 100000。有了这个 id,我需要在我的后端 REST api 上执行 GET 任务,并将返回值设置为 value。谢谢
最佳答案
您可以利用 Rx 运算符,例如 flatMap
和 Observable.forkJoin
/Observable.of
。
这是一个示例:
this.http.get('config.json')
.map(res => res.json())
.flatMap(config => {
return Observable.forkJoin(
Observable.of(config),
// For example for the request. You can build the
// request like you want
this.http.get(
`http://.../${config.rightUpperLogoId.id}`)
);
})
.map(res => {
let config = res[0];
let rightUpperLogoIdValue = res[1].json();
config.rightUpperLogoId.value = rightUpperLogoIdValue;
return config;
})
.subcribe(config => {
// handle the config object
});
本文可以为您提供更多提示(“聚合数据”部分):
关于json - 使用 angular2 从 JSON 中获取键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37408062/