json-typescript-mapper icon indicating copy to clipboard operation
json-typescript-mapper copied to clipboard

deserialize arrays

Open Geschan opened this issue 8 years ago • 7 comments

I tried to deserialize an array of items like this:

this.authHttp.get('/items')
  .map(response => deserialize(Item[], response.json()))
  .catch(this.handleError)
  .subscribe(data => console.log(data));

But the compiler didn't like it. How can I fix this?

Geschan avatar Feb 17 '17 16:02 Geschan

You could try something like

this.authHttp.get('/items')
  .map(response => response.json().map(item => deserialize(Item, item)))
  .catch(this.handleError)
  .subscribe(data => console.log(data));

WilliamChelman avatar Mar 02 '17 16:03 WilliamChelman

@Geschan for now the first parameter of the deserialize method accept object. I will consider your case and add this feature soon.

jf3096 avatar Mar 03 '17 06:03 jf3096

+1 for deserializing arrays

Vertygo avatar Mar 12 '17 22:03 Vertygo

Was there a working solution for deserializing arrays, the solution @WilliamChelman provided produced an error "json is not a function"

rreid116 avatar Sep 08 '17 18:09 rreid116

@rreid116 The this.authHttp.get('/items') return a promise with a response object with the json method on it but you might not be using that particular service / library, do you ? The point of the answer is that, if you have an array (and I mean a real Array, not a string representation), you could always do something like:

let myArray = [...]; 
let myMappedArray = myArray.map(obj => deserialize(MyClass, obj))

WilliamChelman avatar Sep 09 '17 14:09 WilliamChelman

+1 for deserialize array

daveo1001 avatar Mar 16 '18 13:03 daveo1001

Any update on this issue?

michaelsync avatar Mar 19 '19 22:03 michaelsync