Reputation: 119
I build a Model and parse JSON data but there is problem of parsing.
My JSON data looks like this:
{
"receiptNo": [
{
"_id": "5ba6101964991b2f44e1c9cf",
"receiptNo": "21471",
"rollno": 122,
"bankcode": 2,
"userid": "rifat",
"__v": 0
}
]
}
And, this is my model for angular:
export class ReceiptModel
{
receiptNo: String;
}
Please note that I want to get the receiptNo
but unable to get.
How to declare model for this specific JSON?
Upvotes: 0
Views: 29
Reputation: 6260
In TypeScript the property receiptNo: String;
will be expecting a string.
Your data has an Array as the value of this property, so it will not be a good fit.
You might want to create a Receipt
class and change your current class to:
export class ReceiptModel
{
receiptNo: Receipt[];
}
So to access the number from within the array, assuming you have a model like this:
export class Receipt
{
receiptNo: String;
}
you would have to get an element of that array:
const data: ReceiptModel;
data = <your data here>;
const aReceipt = data.receiptNo[0]; //Just getting the first one
//now the receipt number can be accessed via:
aReceipt.receiptNo
Upvotes: 1