将json对象转换为typescript数组

时间:2016-12-20 14:53:02

标签: javascript json typescript

对于我当前的项目,我需要将json对象转换为typescript数组。 json如下:

{
  "uiMessages" : {
    "ui.downtime.search.title" : "Search Message",
    "ui.user.editroles.sodviolation.entries" : "Violated SOD entries"
  },
  "userInfo" : {
    "login" : "fooUser",
    "firstName" : "Foo",
    "lastName" : "Bar",
    "language" : "en"
  },
  "appInfo" : {
    "applicationName" : "foo",
    "applicationVersion" : "6.1.0"
  }
}

json是一个序列化的java对象,uiMessages变量是java中的Hashmap。我需要将uiMessages解析为uiMessage对象的Typescript数组。

到目前为止,我得到了这个:

@Injectable()
export class BootstrapService {

  constructor(private http: Http) {}

  getBootstrapInfo() {
    return this.http.get('api/foo')
      .map(response => {
        response.json()
      })
  }
}

我最好怎么做?

1 个答案:

答案 0 :(得分:0)

试试这个:

@Injectable()
export class BootstrapService {

    constructor(private http: Http) {}

    getBootstrapInfo() {
        return this.http.get('api/foo')
            .map(response => {
                var responseData = response.json();
                var result = [];
                for (let item in responseData.uiMessages) {
                    // Create the uiMessage object here, not sure what its structure is
                    result.push({
                        field: item,
                        message: responseData.uiMessages[item]
                    });
                }
                return result;
            });
    }
}