0

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?

1
  • In your JSON, receiptNo is an array, not a string. Commented Sep 23, 2018 at 6:05

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Then How I get receiptNo inside the array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.