{"action":"created","issue":{"url":"https://api.github.com/repos/angular-buch/book-monkey2-api/issues/2","repository_url":"https://api.github.com/repos/angular-buch/book-monkey2-api","labels_url":"https://api.github.com/repos/angular-buch/book-monkey2-api/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/angular-buch/book-monkey2-api/issues/2/comments","events_url":"https://api.github.com/repos/angular-buch/book-monkey2-api/issues/2/events","html_url":"https://github.com/angular-buch/book-monkey2-api/issues/2","id":331136507,"node_id":"MDU6SXNzdWUzMzExMzY1MDc=","number":2,"title":"Using TypeScript um Json auf Objekte zu \"mappen\"","user":{"login":"idseefeld","id":866917,"node_id":"MDQ6VXNlcjg2NjkxNw==","avatar_url":"https://avatars0.githubusercontent.com/u/866917?v=4","gravatar_id":"","url":"https://api.github.com/users/idseefeld","html_url":"https://github.com/idseefeld","followers_url":"https://api.github.com/users/idseefeld/followers","following_url":"https://api.github.com/users/idseefeld/following{/other_user}","gists_url":"https://api.github.com/users/idseefeld/gists{/gist_id}","starred_url":"https://api.github.com/users/idseefeld/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/idseefeld/subscriptions","organizations_url":"https://api.github.com/users/idseefeld/orgs","repos_url":"https://api.github.com/users/idseefeld/repos","events_url":"https://api.github.com/users/idseefeld/events{/privacy}","received_events_url":"https://api.github.com/users/idseefeld/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-06-11T10:43:59Z","updated_at":"2018-06-11T17:56:23Z","closed_at":null,"author_association":"NONE","body":"Ich bin kürzlich auf eine einfache Methode gestoßen, um Json auf eigene Objekttypen mit dem **as**-Operator zu \"mappen\". Ganz ohne RxJS.map und Factories:\r\nBeispiel-Json:\r\nexample.json\r\n```\r\n{\r\n  \"title\": \"Complexes Objekt\",\r\n  \"persons\": \r\n  [\r\n\t{\r\n\t\t\"id\": 1,\r\n\t\t\"name\": \"Max\"\r\n\t},\r\n\t{\r\n\t\t\"id\": 2,\r\n\t\t\"name\": \"Moritz\"\r\n\t},\r\n\t{\r\n\t\t\"id\": 3,\r\n\t\t\"name\": \"Widow Bolte\"\r\n\t}\r\n  ],\r\n  \"actions\": \r\n  [\r\n\t{\r\n\t\t\"id\": 1,\r\n\t\t\"description\": \"saw gap into bridge\"\r\n\t},\r\n\t{\r\n\t\t\"id\": 2,\r\n\t\t\"description\": \"steal grilled chicken\"\r\n\t}\r\n  ],\r\n}\r\n```\r\n\r\nexample.models.ts\r\n```\r\nexport class Person {\r\n  public id: number;\r\n  public name: string;\r\n}\r\n\r\nexport class Action {\r\n  public id: number;\r\n  public description: string;\r\n}\r\n\r\nexport class Events {\r\n  public title: string;\r\n  public persons: Person[];\r\n  public actions: Action[];\r\n}\r\n```\r\n\r\nexample.services.ts\r\n```\r\nimport { Person, Action, ComplexObj } from '.model.ts';\r\n...\r\n@Injectable()\r\nexport class exampleService {\r\n\t...\r\n\tpublic readAll(): Observable<Events> {\r\n\t\treturn this.http\r\n\t\t  .get('/Api/AllEvents/')\r\n\t\t  .retry(3)\r\n\t\t  .map(response => response.json() as Events)\r\n\t\t  .catch(this.errorHandler);\r\n\t}\r\n\r\n```\r\nDie interessante Teil ist: **.map(response => response.json() as Events)**\r\n\r\nexample.component.ts\r\n```\r\n...\r\nsrc: exampleService;\r\nev: Events;\r\nconstructor(@Inject(exampleService) exampleServiceFactory: any) {\r\n\tthis.src = exampleServiceFactory();\r\n}\r\nngOnInit(){\r\n\tthis.src.readAll().subscribe(res => this.ev = res);\r\n}\r\n```\r\n\r\nFalls ich einen entsprechenden Hinweis nicht überlesen habe, hoffe ich, dass diese eine hilfreiche Anmerkung ist."},"comment":{"url":"https://api.github.com/repos/angular-buch/book-monkey2-api/issues/comments/396330377","html_url":"https://github.com/angular-buch/book-monkey2-api/issues/2#issuecomment-396330377","issue_url":"https://api.github.com/repos/angular-buch/book-monkey2-api/issues/2","id":396330377,"node_id":"MDEyOklzc3VlQ29tbWVudDM5NjMzMDM3Nw==","user":{"login":"idseefeld","id":866917,"node_id":"MDQ6VXNlcjg2NjkxNw==","avatar_url":"https://avatars0.githubusercontent.com/u/866917?v=4","gravatar_id":"","url":"https://api.github.com/users/idseefeld","html_url":"https://github.com/idseefeld","followers_url":"https://api.github.com/users/idseefeld/followers","following_url":"https://api.github.com/users/idseefeld/following{/other_user}","gists_url":"https://api.github.com/users/idseefeld/gists{/gist_id}","starred_url":"https://api.github.com/users/idseefeld/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/idseefeld/subscriptions","organizations_url":"https://api.github.com/users/idseefeld/orgs","repos_url":"https://api.github.com/users/idseefeld/repos","events_url":"https://api.github.com/users/idseefeld/events{/privacy}","received_events_url":"https://api.github.com/users/idseefeld/received_events","type":"User","site_admin":false},"created_at":"2018-06-11T17:56:23Z","updated_at":"2018-06-11T17:56:23Z","author_association":"NONE","body":"Hallo Ferdinand,\r\nganz herzlichen Dank für die unerwartet schnell und ausführliche Antwort!\r\nDas macht die Thematik noch deutlicher. Und wo ich schon dabei bin - vielen Dank für das gute Buch auch an Deine Co-Autoren bzw. das ganz Team!\r\nViele Grüße\r\nDirk"}}
{"actor":{"display_login":"idseefeld"}}