第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決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": "a@ma.co"      }    }}json_decode($string);例如“名稱”:abc,我只想打印 abc
查看完整描述

3 回答

?
慕碼人2483693

TA貢獻1860條經(jīng)驗 獲得超9個贊

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


$string= '{

  "data": {

    "id": 123,

    "name": "abc",

    "gsm": 1133,

    "metadata": {

      "cart_id": 13243

    },

    "customer": {

      "id": 123,

      "email": "a@ma.co"

    }

  }

}';


$response = json_decode($string);

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

die;

希望這對你有幫助。


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

TA貢獻1816條經(jīng)驗 獲得超6個贊

您可以將 JSON 轉(zhuǎn)換為數(shù)組并通過鍵訪問元素

$jArr = json_decode($string, true);

訪問元素

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


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

TA貢獻1719條經(jīng)驗 獲得超6個贊

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


$JSON = '{

    "data":{

        "id":123,

        "name": abc,

        "gsm":1133,

        "metadata":{

            "cart_id":13243

        },

        "customer":{

            "id":123,

            "email": "a@ma.co"

        }

    }

}'; //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'];

此代碼打?。?/p>


Printing the customer id using an object: 123


Printing the customer id using an array: 123


查看完整回答
反對 回復(fù) 2022-01-02
  • 3 回答
  • 0 關(guān)注
  • 186 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號