json - 使用 angular2 从 JSON 中获取键和值

标签 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 任务,并将返回值设置为 value。谢谢

最佳答案

您可以利用 Rx 运算符,例如 flatMapObservable.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/

相关文章:

python - 将 Django Rest Framework 与序列化程序一起使用时出现 AttributeError

java - 跨 weblogic 集群环境的序列化测试

typescript - 如何在 Typescript 中输入检查二进制类型别名

typescript - Angular 2 : How to Prevent my data and html view by not reloading component on browser back navigation

javascript - React 如何使用 setState 和 Map 值

c# - 序列化 DataAnnotations

Python HTML 表转 JSON

java - JSONArray 未正确返回

java - 如何将 JSON 响应包装在父对象中

javascript - 在 JavaScript 中执行 pg_query 时 PHP 数组中出现 JSON 编码错误