亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何打印解析為 php 對象的 json 字符串

如何打印解析為 php 對象的 json 字符串

PHP
手掌心 2022-01-02 17:20:31
我有一個 JSON 字符串,我將其解析為 PHP 對象,我想從字符串中打印某些值,如何打印我需要的確切值。我提供了下面的代碼{  "data":    {      "id":123,       "name": "abc",       "gsm":"1133",       "metadata":       {        "cart_id":13243      },       "customer":       {        "id":123,        "email": "[email protected]"      }    }}json_decode($string);例如“名稱”:abc,我只想打印 abc
查看完整描述

3 回答

?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

您的 JSON 字符串無效,您在其中缺少括號。此外,字符串值必須在 json 中包含引號。你的字符串應該像


$string= '{

  "data": {

    "id": 123,

    "name": "abc",

    "gsm": 1133,

    "metadata": {

      "cart_id": 13243

    },

    "customer": {

      "id": 123,

      "email": "[email protected]"

    }

  }

}';


$response = json_decode($string);

print_r($response->data->name);

die;

希望這對你有幫助。


查看完整回答
反對 回復 2022-01-02
?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

您可以將 JSON 轉換為數組并通過鍵訪問元素

$jArr = json_decode($string, true);

訪問元素

$jArr['data']['name']


查看完整回答
反對 回復 2022-01-02
?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

您必須從 json 解碼訪問元素,如下所示:


$JSON = '{

    "data":{

        "id":123,

        "name": abc,

        "gsm":1133,

        "metadata":{

            "cart_id":13243

        },

        "customer":{

            "id":123,

            "email": "[email protected]"

        }

    }

}'; //There were three errors, you have to to use ' insted of " for including the JSON string, you missed one '}' and you forgot the "" in the 'email' value under 'customer' section


$object = json_decode($JSON);

echo 'Printing the customer id using an object: '.$object->data->customer->id.PHP_EOL.PHP_EOL;


$array = json_decode($JSON, true);

echo 'Printing the customer id using an array: '.$array['data']['customer']['id'];

此代碼打印:


Printing the customer id using an object: 123


Printing the customer id using an array: 123


查看完整回答
反對 回復 2022-01-02
  • 3 回答
  • 0 關注
  • 200 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號