【问题标题】:Extract data from JSON with PHP?用 PHP 从 JSON 中提取数据?
【发布时间】:2021-10-21 03:11:43
【问题描述】:

我想从这个 Json 中获取用户名(david)的数据,我看到了类似的问题,我尝试了 100 多次但我做不到,如果可能的话,我需要准确的答案。 提前致谢

{
    "related_assets": [],
  "orders": [],
  "auctions": [],
  "supports_wyvern": true,
  "top_ownerships": [
    {
      "owner": {
        "user": {
          "username": "david"
        },
        "",
        "address": "",
        "config": ""
      },
      "quantity": "1"
    }
  ],
  "ownership": null,
  "highest_buyer_commitment": null
}

我的编码如下:

<?php

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
    //the second parameter turns the objects into an array
    $decodedData = json_encode($response, true);
    $owner = $decodedData['top_ownerships'][0]['owner'];
    $username = $owner['user']['username'];
    echo $username;


}

输出是“ 请帮忙谢谢

【问题讨论】:

标签: php json curl decode


【解决方案1】:

您的代码完全是最新的!它就像魅力一样!

但是您在 JSON 代码中犯了一个非常模棱两可的错误

你使用这个 ” 而不是 " (双引号) 那里 "david"

它们看起来完全一样!

您还错过了 JSON 代码开头和结尾的 {}

checkout your json code here it's the best tool for this

<?php

$code = '{
    "top_ownerships": [{
        "owner": {
            "user": {
                "username": "david"
            },
            "profile_img_url": "",
            "address": "",
            "config": ""
        },
        "quantity": "1"
    }]
}';

  $decodedData = json_decode($code, true);

  $owner = $decodedData['top_ownerships'][0]['owner'];
  $username = $owner['user']['username'];
  echo $username;

?>

【讨论】:

    【解决方案2】:

    首先,了解如何访问 json 字段和 php 键很重要;

    假设上述值存储在名为data.json 的文件中,我们可以这样提取值:

    $data = file_get_contents('data.json');
    
    //the second parameter turns the objects into an array
    $decodedData = json_decode($data, true);
    
    $owner = $decodedData['top_ownerships'][0]['owner'];
    
    $username = $owner['user']['username'];
    
    //echo $username
    
    

    如果您的 json 文件或数据正确编码,上述方法将有效。

    【讨论】:

    • "", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $错误; } else { $decodedData = json_decode($response, true); $owner = $decodedData['top_ownerships'][0]['owner']; $username = $owner['user']['username'];回显$用户名; }
    【解决方案3】:

    首先你必须将你的 JSON 存储到一个变量中,我们称之为 $json。

    $jsonArray = json_decode($json,true);
    

    并设置您的密钥名称,即您想要从 JSON 中获取的密钥。

    $key = "owner";
    $owner= $jsonArray[$key];
    

    你也可以像一个对象一样阅读:

    $owner= $jsonObj->$key;
    

    【讨论】:

      【解决方案4】:

      试试

      $object = json_decode('{"owner": {"user": {"username": "david”},"profile_img_url": "","address": "","config": ""},"quantity": "1"}')
      
      echo $object->owner->user->username
      

      这将使 JSON 成为您可以使用 -> 访问器访问的对象

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-19
        • 2018-10-31
        • 1970-01-01
        • 1970-01-01
        • 2014-07-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多