0

i want to take data of username(david) from this Json , I saw the similar question i try more than 100 times but i can not , I need exact answer if it's possible. thanks in advance

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

and i coded as below :

<?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;


}

the output is " please help thanks

Mehdi
  • 1
  • 3
  • 2
    Does this answer your question? [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – spinsch Aug 19 '21 at 12:08

4 Answers4

0

To start with, it's important you understand how to access json fields and key php;

Assuming the above values are stored in a file known as data.json, we can extract the values like this:

$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

The above would work if your json file or data is properly encoded.

  • "", 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 { $decodedData = json_decode($response, true); $owner = $decodedData['top_ownerships'][0]['owner']; $username = $owner['user']['username']; echo $username; } – Mehdi Aug 19 '21 at 14:15
0

first you have to store your JSON into a variable, lets call it $json.

$jsonArray = json_decode($json,true);

And set your key name, the key you want from your JSON.

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

You also can read like an object:

$owner= $jsonObj->$key;
0

Try

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

echo $object->owner->user->username

This will make the JSON an object that you can access by using the -> accessor

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Jordan Casey
  • 955
  • 9
  • 16
0

your code is completely current! and it works like charm!

but you made a very ambiguous mistake in your JSON code

you're using this ” instead of " (double quotes) there "david” <----

they exactly look the same!

and also you missed {} in the beginning and end of your JSON code

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;

?>
Cyrus Raoufi
  • 526
  • 1
  • 3
  • 27