如何从此 JSON 访问产品标题

How to access product title from this JSON

我检索了一些这样的数据:

object(stdClass)[1]
  public 'status' => string 'ok' (length=2)
  public 'data' => 
    object(stdClass)[3]
      public 'sort_data' => 
        object(stdClass)[2]
          public 'sort_column' => string 'order_item_id' (length=13)
          public 'sort_order' => string 'asc' (length=3)
      public 'pager' => 
        object(stdClass)[4]
          public 'page' => int 1
          public 'item_per_page' => int 50
          public 'total_page' => int 1
          public 'total_rows' => int 21
      public 'form_data' => 
        array (size=0)
          empty
      public 'items' => 
        array (size=21)
          0 => 
            object(stdClass)[5]
              public 'order_item_id' => int 323360064
              public 'order_id' => int 111179028
              public 'variant' => 
                object(stdClass)[6]
                  public 'id' => int 17586275
                  public 'seller_id' => int 186764
                  public 'site' => string 'digikala' (length=8)
                  public 'is_active' => boolean true
                  public 'is_archived' => boolean false
                  public 'title' => string 'Mug' (length=115)
                  public 'product' => 
                    object(stdClass)[7]
                      public 'id' => int 3634323
                      public 'category_id' => int 6289
                      public 'title' => string 'Mug model series' (length=40)
                      public 'shipping_nature_id' => int 1
                      ...

这背后的代码在这里:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'site_url',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json, application/json;charset=UTF-8',
    'Authorization: {{token}}'
  ),
));

$response = curl_exec($curl);

var_dump(json_decode($response));

现在我需要分别访问这些值中的每一个。例如我想得到 public 'title' => string 'Mug model series'。我该怎么做?

您可以告诉 json_decode() 到 return 数组而不是对象:

$response = json_decode($response,true);

Now I need to access each of these values separately. For example I want to get public 'title' => string 'Mug model series'

...
$object = json_decode($response);

echo $object->data->items[0]->variant->product->title;
// Mug model series

[0]表示数组中的第一项,所以如果要循环它:

foreach ($object->data->items as $item) 
  echo $item->variant->product->title;

首先你需要使用

将对象转换为数组
$response = json_decode($response, true);

解码响应并获取数组而不是对象后,您可以顺便输入以下内容来访问数据

echo $response->data->items[0]->order_id;
// output : 111179028

假设您必须在 table 中显示每个数据,或者您需要这样的 foreach 循环:

foreach($response->data->items as $item){
?>
 <h1>
  <?php 
   echo $item->variant->seller_id; 
  ?>
 </h1>
 <h2>
  <?php
   echo $item->variant->product->title
  ?>
 </h2>
<?php
}
?>

你可以修改页面的视图或显示你想要的信息如果你需要更多信息你可以访问下面的链接

https://www.php.net/manual/en/function.json-decode.php

https://www.w3schools.com/php/php_json.asp

希望这个答案对您有用

试试这个

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

解码 JSON 字符串

var_dump(json_decode($response))
$json= json_decode($json,true);

注意:如果您只是像这样打印数据,将来会更容易。

echo "<pre>";
print_r($response);

现在已经解决了,考虑到您所做的事情,您收到的数据似乎是下面的数据json_decode($response, true)(正确,因为第二个参数为您提供了关联数组)。

[
 "parentStdClass" => [  // (Your question starts from here as stdclass has index 1 probably array)
     ....
     "data" => [
           ...unrelatedData removed form this eg,
           "items" => [ // you have 21 items
             .... 20 items,
             [

                 ...,
                 "variant" => [
                    ...,
                    "title" => "Some title", //variant title

                    "product" => [ // 7 items are here
                       ...,
                       [
                           'title' => 'Mug model series',
                       ] 
                    ]
                 ]
             ],

   
           ],
     ]
 ]
]  

现在要获得您需要做的第一个产品的标题。

$response = json_decode($response, true);
// Put what the key name is instead of 0 if it's associative array
$questionStartingArray = $response[0];

$data = $questionStartingArray['data'];
// Give you all items
$items = $questionStartingArray['items'];

// Gives you first item variant & title
$variant = $items[0]['variant'];
$variantTitle = $variant['title'];

// Gives you first variants product  & product title
$product = $variant['product'];
$productTitle = $product[0]['title'];
$allProductTitleInsideVariant = array_reduce($product , function($carry, $item){
   $carry[] = $item['title'];
   return $carry;
},[]);

如果您想要一个项目中的所有产品标题,则通过循环获取它。例如:

$productTitle = [];
$variantTitle = [];
foreach($items as $item) {
   foreach($item['variant'] as $variantItem) {
      $variantTitle[] = $variantItem['title'];
      foreach($variantItem['product'] as $productItem) {
         $productTitle[] = $productItem['title'];
      } 

   } 
}

echo "<pre>";
print_r($productTitle);
print_r($variantTitle);

您将在项目数组中获得所有产品标题。