-3

I have a JSON message, and I dont know How can I write out a part of json.

I tried:

{{$data[0]->items[0]}}

{{$data[0]->name}}

{{$data->items[0]-name}}

{{$data[0]->items}}

ect...

JSON message:
{ 
   "items":[ 
      { 
         "name":"Knight",
         "id":26000000,
         "maxLevel":13,
         "iconUrls":{ 
            "medium":"https:\/\/api-assets.clashroyale.com\/cards\/300\/jAj1Q5rclXxU9kVImGqSJxa4wEMfEhvwNQ_4jiGUuqg.png"
         }
      },
      { 
         "name":"Archers",
         "id":26000001,
         "maxLevel":13,
         "iconUrls":{ 
            "medium":"https:\/\/api-assets.clashroyale.com\/cards\/300\/W4Hmp8MTSdXANN8KdblbtHwtsbt0o749BbxNqmJYfA8.png"
         }
      }
   ]
}

EDIT:

This is the Controller

As you see $data array is decoded

It looks like your post is mostly code; please add some more details. omg

     $token = "token";

     $url = "https://api.clashroyale.com/v1/cards";

     $ch = curl_init($url);

     $headr = array();
     $headr[] = "Accept: application/json";
     $headr[] = "Authorization: Bearer ".$token;
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);

     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     $res = curl_exec($ch);
     $data = json_decode($res, true);
     curl_close($ch);

     return view('clash', ['data' => $data]);
Antarax
  • 11
  • 3
  • This is json decoded array. I try to out example only "Knight" – Antarax Feb 19 '20 at 15:43
  • which part of the data are you actually trying to output, exactly? Assuming you've decoded the JSON before this point, and assuming you do actually want the "name" from the first item (as appears likely), then `$data->items[0]->name` is likely to work. Check that you understand object property syntax, and array access syntax in PHP - it's just a PHP syntax error really, JSON has nothing to do with it directly. If that still doesn't work, then please `var_dump()` the PHP version of the variable, then we can see more clearly. – ADyson Feb 19 '20 at 15:43
  • SHow us how you `json_decode()` the JSONString, did you make it into an array or did you leave it as the object it is supposed to be – RiggsFolly Feb 19 '20 at 15:45
  • $data->items[0]->name Trying to get property 'items' of non-object – Antarax Feb 19 '20 at 15:45
  • like I said already... _ If that still doesn't work, then please var_dump() the PHP version of the variable, then we can see more clearly_. Don't just post the error (i.e symptom), post the data it came from, then we can actually diagnose it. (But...likely it's an associative array instead of an object, so even without seeing it, I'd suggest trying `$data["items"][0]["name"]` instead.) – ADyson Feb 19 '20 at 15:47
  • P.S. The options you pass to json_decode() can also determine the exact structure of PHP variable which is produced as a result. You didn't show us these either, so we could not predict the exact result of decoding your data. Too much info is missing from your question overall...hence why we are mostly making suggestions and instead of giving definite answers. See https://stackoverflow.com/help/mcve for future reference when asking questions. – ADyson Feb 19 '20 at 15:49
  • Ah thanks for the update. `json_decode($res, true);`... the `true` (as per the [manual](https://php.net/manual/en/function.json-decode.php)) tells PHP to create an associative array as the result, instead of objects. Hence why object-property syntax (i.e. `$obj->property`) does not work, and array syntax (i.e. `$obj["property"]`) does. Did you notice this point in the documentation? Have you familiarised yourself with the correct PHP syntax for manipulating objects, arrays etc? – ADyson Feb 19 '20 at 15:53
  • Anyway... http://sandbox.onlinephpfunctions.com/code/ebe8410dec07184f7a0fd82940e26b826c6161c4 – ADyson Feb 19 '20 at 15:56
  • Ahh, $data["items"][0]["name"] this work, thank you very much! The "true" is not my work, thanks for the help! – Antarax Feb 19 '20 at 15:57
  • 1
    Do you really have a function stranded in the middle of knowhere in that code _Which incidentally does absolutely NOTHING_ – RiggsFolly Feb 19 '20 at 16:00
  • 1
    "The "true" is not my work"...nonetheless, you could still have a) gone and found out what it does, and/or b) done a var_dump() of the $data variable, as I suggested, to discover its true structure (which might have then led you to wonder why it was structured like that, which would have taken you back to the json_decode command). None of what I and others have written above is information which cannot already be discovered by some simple research and simple debugging. On this site we find ourselves continuously repeating the same basic, researchable stuff over and over and over again. – ADyson Feb 19 '20 at 16:00
  • 1
    @ADyson Well said – RiggsFolly Feb 19 '20 at 16:01

1 Answers1

1

First you have to decode your JSON-String to a PHP-Array and then you can access it easily this way:

$json = '{
   "items":[
      {
         "name":"Knight",
         "id":26000000,
         "maxLevel":13,
         "iconUrls":{
            "medium":"https:\/\/api-assets.clashroyale.com\/cards\/300\/jAj1Q5rclXxU9kVImGqSJxa4wEMfEhvwNQ_4jiGUuqg.png"
         }
      },
      {
         "name":"Archers",
         "id":26000001,
         "maxLevel":13,
         "iconUrls":{
            "medium":"https:\/\/api-assets.clashroyale.com\/cards\/300\/W4Hmp8MTSdXANN8KdblbtHwtsbt0o749BbxNqmJYfA8.png"
         }
      },
      {
         "name":"Goblins",
         "id":26000002,
         "maxLevel":13,
         "iconUrls":{
            "medium":"https:\/\/api-assets.clashroyale.com\/cards\/300\/X_DQUye_OaS3QN6VC9CPw05Fit7wvSm3XegXIXKP--0.png"
         }
      },
      {
         "name":"Giant",
         "id":26000003,
         "maxLevel":11,
         "iconUrls":{
            "medium":"https:\/\/api-assets.clashroyale.com\/cards\/300\/Axr4ox5_b7edmLsoHxBX3vmgijAIibuF6RImTbqLlXE.png"
         }
      }
   ]
}';

$array = json_decode( $json, true ); // we receive an associative array because the second parameter is true

echo $array['items'][0]['name'];
echo $array['items'][1]['id'];

Usage in for example:

$token = "token";
$url = "https://api.clashroyale.com/v1/cards";
$ch = curl_init($url);

$headr = array();
$headr[] = "Accept: application/json";
$headr[] = "Authorization: Bearer ".$token;

curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$res = curl_exec($ch);
$data = json_decode($res, true);
curl_close($ch);

echo $data['items'][0]['name']; // echo the value of the key 'name' of the first element in items
echo $data['items'][1]['id']; // echo the value of the key 'id' of the second element in items
// you can also store them or do whatever you want

return view('clash', ['data' => $data]);

Or access the data in your view like this:

{{ $data['items'][0]['name'] }}
{{ $data['items'][0]['id'] }}
Robin Gillitzer
  • 1,603
  • 1
  • 6
  • 17