【问题标题】:Getting key and value from JSON with angular2使用 angular2 从 JSON 获取键和值
【发布时间】:2016-09-21 08:12:04
【问题描述】:

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

【问题讨论】:

    标签: json serialization typescript angular


    【解决方案1】:

    您可以利用 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
           }); 
    

    这篇文章可以给你更多的提示(“聚合数据”部分):

    【讨论】:

    • 复制和粘贴,小定制,一切都很完美。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多