How to assign a Json font within an interface

Asked

Viewed 52 times

0

I am trying to generate an interface of a Json file but this error, what would be the error in my code?

 export interface Compra {
        event: string;
        timestamp: number;
        custom_data: number;
        value: string;
        key: number;
    }




{"events":[{"event":"comprou-produto",
                "timestamp":"2017-09-22T13:52:24.2311892-30:00",
                "custom_data":[{"key":"product_name",
                                "value":"Pants"},
                        {"key":"transaction_id","value":"135154"},
                               {"key":"product_price","value":131}]},

               {"event":"comprou",
                "timestamp":"2016-09-22T13:57:31.2311892-03:00",
                "revenue":250,
                "custom_data":[{"key":"store_name","value":"Loja y"},{"key":"transaction_id","value":"3029384"}]},

               {"event":"comprou-produto","timestamp":"2016-09-22T13:57:33.2311892-03:00","custom_data":[{"key":"product_price","value":150},{"key":"transaction_id","value":"3216545"},{"key":"product_name","value":"Shirt"}]},

               {"event":"comprou-produto","timestamp":"2016-10-02T11:37:35.2300892-03:00","custom_data":[{"key":"transaction_id","value":"3409340"},{"key":"product_name","value":"Shoes"},{"key":"product_price","value":120}]},

               {"event":"comprou","timestamp":"2016-10-02T11:37:31.2300892-03:00","revenue":120,"custom_data":[{"key":"transaction_id","value":"32132131"},{"key":"store_name","value":"Loja x"}]}]}`insira o código aqui`
  • What error are you making? * Detail I noticed, your custom_data attribute is as number, but it’s actually an object *

  • I’m trying to make this html structure:

  • <span class="content-date">{purchase.timestamp}</span> <H3 class="content-sumprice">{purchase.custom_data}</H3> <H3 class="content-store">{purchase.value}}</H3>

  • <div *ngFor="Let compra of Events"> <tl-compra [compra]="compra"></tl-compra> </div>

  • Ta giving this error: ERROR Typeerror: Cannot read Property

1 answer

1

You need to declare two interfaces and use one inside the other:

export interface Compra {
        event: string;
        timestamp: number;
        custom_data: CustomData[];
    }


export interface CustomData{
        value: string;
        key: number;
    }
  • Okay, thanks for the tip, my output in html is just like I should represent Customdata: <span class="content-date">{purchase.timestamp.key}</span> <H3 class="content-store">{purchase.value}</H3> <!-<H3 class="content-sumprice">{purchase.custom_data}</H3>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.