php 使用php读取Json数据响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17865544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:35:34  来源:igfitidea点击:

Read Json data response using php

phpjsonjanrain

提问by rocksphp

How can I read a JSON data response using php? The response t comes after user authentication done from a third party. Primarily, I just want displayNameand preferredUsernamedata.

如何使用 php 读取 JSON 数据响应?响应 t 在第三方完成用户身份验证后出现。首先,我只想要displayNamepreferredUsername数据。

Json response:

杰森回复:

 {
      "stat": "ok",
      "profile": {
        "providerName": "testing",
        "identifier": "http://testing.com/58263223",
        "displayName": "testing",
        "preferredUsername": "testing",
        "name": {
          "formatted": "testing"
        },
        "url": "http://testing.com/testing/",
        "photo": "https://securecdn.testing.com/uploads/users/5826/3223/avatar32.jpg?1373393837",
        "providerSpecifier": "testing"
      }
    }

回答by vee

You can use the json_decode(http://php.net/manual/en/function.json-decode.php) function to decode your result then retrieve the value:

您可以使用json_decode( http://php.net/manual/en/function.json-decode.php) 函数解码您的结果然后检索值:

$json_data = '{
      "stat": "ok",
      "profile": {
        "providerName": "testing",
        "identifier": "http://testing.com/58263223",
        "displayName": "testing",
        "preferredUsername": "testing",
        "name": {
          "formatted": "testing"
        },
        "url": "http://testing.com/testing/",
        "photo": "https://securecdn.testing.com/uploads/users/5826/3223/avatar32.jpg?1373393837",
        "providerSpecifier": "testing"
      }
    }';

$json = json_decode($json_data);

echo $json->profile->displayName;
echo $json->profile->preferredUsername;

回答by vee

 <?php
$json='{
      "stat": "ok",
      "profile": {
        "providerName": "testing",
        "identifier": "http://testing.com/58263223",
        "displayName": "testing",
        "preferredUsername": "testing",
        "name": {
          "formatted": "testing"
        },
        "url": "http://testing.com/testing/",
        "photo": "https://securecdn.testing.com/uploads/users/5826/3223/avatar32.jpg?1373393837",
        "providerSpecifier": "testing"
      }
    }';


    $data=json_decode($json ,true);
     $preferredUsername=$data['profile']['preferredUsername'];
     $displayName=$data['profile']['displayName'];

    ?>

回答by eclipsis

json_decodeis what you're looking for:

json_decode是您要查找的内容:

$json = '[
    {
        "displayName": "testing",
        "preferredUsername": "testing",
    }
]';

$jsonArray = json_decode($json);

foreach($jsonArray as $value){
    $displayName = $value->Display Name;
    $preferredUsername = $value->Preferred User;
}