I am working with a json file as shown below:
{
"offers": [
{
"offerId": "5",
"retailerId": "0"
},
{
"offerId": "6",
"retailerId": "1"
},
{
"offerId": "7",
"retailerId": "2"
}
]
}
I am used to working with OOP in Java and C# and would like to know if it is possible to parse this json object to a class so I can easily work with it in PHP. As you can see each offer is nested in the offers parent. This parent is redundant, is there a way to save al the nested objects in an array which has objects of type Offer (php class)?
json_decode()will return objects of classstdClass. I don't think there's a built-in way to create custom classes.OffersCollectionclass that enforces types of its members, but that itself may be overkill for your purposes. TBH most of time we just assume that the array will contain the things it is supposed to, optionally verifying after the fact.