0

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?

3
  • Do you have a definition of uiMessage in TypeScript? Commented Dec 20, 2016 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. Commented Dec 20, 2016 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" Commented Dec 21, 2016 at 6:27

1 Answer 1

0

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;
            });
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.