1

For my current project I need to convert a json object to a typescript array. the json is as follows:

{
  "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"
  }
}

The json is a serialized java object and the uiMessages variable is a Hashmap in java. I need to parse the uiMessages to a Typescript Array of uiMessage objects.

So far, I got to this:

@Injectable()
export class BootstrapService {

  constructor(private http: Http) {}

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

How would I best do this?

Ronny176
  • 265
  • 1
  • 4
  • 13
  • Do you have a definition of `uiMessage` in TypeScript? – Heretic Monkey Dec 20 '16 at 14:57
  • Please provide the format of the array you are trying to create. `uiMessages` is an object with keys and values--there's no obvious mapping from that to an array, which is just an indexed list of values. –  Dec 20 '16 at 19:07
  • the uiMessage would be a simple key-value object, where the key would be for instance "ui.downtime.search.title" and the value "Search Message" – Ronny176 Dec 21 '16 at 06:27

1 Answers1

1

Try this:

@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;
            });
    }
}
Robba
  • 7,684
  • 12
  • 48
  • 76