使用angular2从JSON获取键和值

时间:2016-05-24 08:22:28

标签: json serialization typescript angular

我正在寻找如何在我的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任务,并且我想要设置为值的返回值。谢谢

1 个答案:

答案 0 :(得分:4)

您可以利用flatMap / Observable.forkJoin之类的Observable.of等Rx运算符。

以下是一个示例:

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
       }); 

本文可以为您提供更多提示(第34节;汇总数据"):