簡體   English   中英

JMS序列化器反序列化嵌套對象

[英]JMS Serializer deserialize nested objects

我正在使用: https : //jmsyst.com/libs/serializer

用於反序列化json對象。 目前我有這個:

/**
 * @param int $id
 * @return Customer
 * @throws \Http\Client\Exception
 */
public function get(int $id): Customer
{
    $response = $this->client->get('/customers/' . $id);

    $data = json_encode(json_decode(
        $response->getBody()->getContents()
    )->data->attributes);

    return $this
        ->serializer
        ->deserialize($data, Customer::class, 'json');
}

我在這里收到的json看起來像這樣:

   {
    "data": {
            "type": "customer",
            "id": "4356456",
            "links": {
                    "self":"https:\/\/api.ecurring.com\/customers\/345656"
            },
            "attributes": {
                    "gender": "m",
            "first_name": "Laurens"
        }
    }

是否可以告訴JMS它應該自動從data->attributes開始,而不是像這樣做一些骯臟的事情:

 $data = json_encode(json_decode(
            $response->getBody()->getContents()
        )->data->attributes);

您可以檢查排除政策。

閱讀文檔,這真的很有幫助。

排除政策注釋

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM