Problem in JSON request - API - json_decode

Asked

Viewed 88 times

0

Greetings, I’m trying to request a JSON so I can attach each value in a variable, conduit, I’m getting the errors:

Notice: Trying to get Property 'product' of non-object in E: Rafael Desktop T.I Xamp htdocs litoralplace backend productshow.php on line 14

Warning: Invalid argument supplied for foreach() in E: Rafael Desktop T.I Xamp htdocs litoralplace backend productshow.php on line 14

My code:

<?php
    $json_file = file_get_contents('produtos.json', true);
$codigo; $descricao; $tipo;
    $json_str = json_decode($json_file);
foreach ($json_str->retorno->produtos->produto as $key) {
    $codigo = $key->codigo;
    $descricao = $key->descricao;
    $tipo = $key->tipo;
}
?>

unfortunately I can not make available the.json products file as it has confidential company information. I will just exemplify it:

{
"retorno": {
  "produtos": [
    {
     "produto": {
        "codigo": "1",
        "descricao": "Fonte Thermaltake 430W Smart Séries",
        "tipo": "P"
     },
     "produto": {
        "codigo": "2",
        "descricao": "Processador AMD Ryzen 3 3200G",
        "tipo": "P",
     }
    }
   ]
  }
 }

In JSON there are 100 Products, I am trying to insert in the variables only one product and its parameters. I’m sorry if you got a little confused, I really need this help.

Illustrating what I need: I need to take the values of each Product (code, description and type), and attach in an array. For example: Code array: Needs to be filled in with code 1 and 2. My question is not knowing how to differentiate these two "product" to gather their information, since JSON was formatted in this way via an API.

  • This json is incorrect

  • complicated, it is the fruit of an API, came so already. How should I fix it?

  • Now, I’ve seen, you’ve got the wrong keys, I’ll fix it. Anyway, that bug isn’t really in my code.

  • Gives a print_r($json_str); die; and put the comeback.

  • php&#xA;stdClass Object&#xA;(&#xA; [retorno] => stdClass Object&#xA; (&#xA; [produtos] => Array&#xA; (&#xA; [0] => stdClass Object&#xA; (&#xA; [produto] => stdClass Object&#xA; (&#xA; [codigo] => 2&#xA; [descricao] => Processador AMD Ryzen 3 3200G&#xA; [tipo] => P&#xA; )&#xA;&#xA; )&#xA;&#xA; )&#xA;&#xA; )&#xA;&#xA;)&#xA; here friend

  • Your foreach is to go up $json_str->retorno->produtos. produto is an item of the product array.

  • got it!! Good, thank you very much. However, how should I differentiate and go through the indexed items each Product that is in the products array?

  • I’m sorry but I couldn’t understand what you want to do, please if possible edit the question and try to exemplify what you want to do.

  • I’ll do it. Thank you

  • I edited there, if you can take a look, thank you.

Show 5 more comments

1 answer

0


If I understand correctly you want to create a array only with the product codes. Then the code below should help you to do this.

$json_file = file_get_contents('produtos.json', true);
$codigos = [];
$json_str = json_decode($json_file);

foreach ($json_str->retorno->produtos as $value) {
    $codigos[] = $value->produto->codigo;
}

print_r($codigos); die;

// O exemplo irá imprimir
Array
(
    [0] => 1
    [1] => 2
)
  • Returned an Undefined Property error: Undefined Property: stdClass::$codigo in E: Rafael Desktop T.I Xamp htdocs litoralplace backend productshow.php on line 16 $json_file = file_get_contents('contacts.json', true); $codigos = []; $json_str = json_decode($json_file); foreach ($json_str->return->products as $product) { $codes[] = $product->code; //line 16 } print_r($code); die;

  • Test the following: $produto->produto->codigo

  • now it worked, but returned only one item: Array ( [0] => 2 )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.