PHP Classes

File: doc/3_mapping_alternative_property_names.md

Recommend this page to a friend!
  Classes of Julian Finkler   JSON Object Mapper   doc/3_mapping_alternative_property_names.md   Download  
File: doc/3_mapping_alternative_property_names.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: JSON Object Mapper
Create objects of classes mapped from JSON strings
Author: By
Last change:
Date: 5 years ago
Size: 850 bytes
 

Contents

Class file image Download

Mapping Alternative Property Names

? Go to index

Sometimes it is useful or required to map the property names from the json to another property name in the class object. (e.g. the property uses incompatible characters or the naming is not good):

{
    "first name": "Pete",
    "lastname": "Peterson",
    "foo*bar": "baz"
}

In this case you can use the name argument in the annotation to refer the name of the property in the json object

<?php

class User {
    / @JsonField(name="first name") */
    public $firstname;

    / @JsonField(name="lastname") */
    public $surname;

    / @JsonField(name="foobar")/
    public $aFunnyMessage;
}

The mapped object would be something like that:

User Object
(
    [firstname] => Pete
    [surname] => Peterson
    [aFunnyMessage] => baz
)