castable icon indicating copy to clipboard operation
castable copied to clipboard

JSON Array cast

Open mobilefocustoar opened this issue 7 years ago • 5 comments

How are you. I think that this library is useful.

But, I have a problem in programming. I need your help.

If the result is json object, and it is simple: { "type":"xxx", "currency":"btc", "amount":"0.0", "available":"0.0" }

class is following: export class Response extends Castable { @cast type: string; @cast currency: string; @cast amount: number; @cast available: number; }

But json response is array. There is no key. the json response is following: [{ "type":"xxx", "currency":"btc", "amount":"0.0", "available":"0.0" },{ "type":"xxx", "currency":"usd", "amount":"1.0", "available":"1.0" },{ "type":"xxx", "currency":"btc", "amount":"1", "available":"1" }]

what is class for castable? Please teach me.

Regards.

mobilefocustoar avatar Jun 06 '18 09:06 mobilefocustoar

Hi, Is my question stupid?

mobilefocustoar avatar Jun 18 '18 20:06 mobilefocustoar

Hi. Did you create Response object like this?

const response = new Response(JSON.parse(serverResponse));

Castable don't change json responses directly.

Regards.

undrthemt avatar Jun 20 '18 05:06 undrthemt

Thanks for your response.

Yes, I create response like above. I want to confirm that above is right.

Regards.

mobilefocustoar avatar Jun 24 '18 07:06 mobilefocustoar

I want to know how program code when root of response is array.

Thank you.

mobilefocustoar avatar Jun 24 '18 08:06 mobilefocustoar

const parsedArray = (JSON.parse(serverResponse) as Array<unknown>).map(
  (elem) => new Response(elem)
);

jcmonteiro avatar Mar 26 '23 07:03 jcmonteiro