Home > database >  Parsing JSON with PHP Amazon
Parsing JSON with PHP Amazon

Time:09-30

I try to get the DisplayValues from Json code with PHP but I can't do it.

{
    "ItemInfo": {
        "Features": {
            "Label": "Features",
            "Locale": "en_US",
            "DisplayValues": [
                "Vented clear cover doubles as a 6-quart capacity popcorn bowl",
                "Stir rod is motorized and improves popping, get more popped corn, larger kernels per batch",
                "Convenient nesting lid is ideal for small storage"
            ]
        }
    }

To get Locale I do this:

// Convert JSON string to Array
$someArray = json_decode($response, true);

$productosMaximos = count($someArray['SearchResult']['Items']);

$productosMaximosResta = ($productosMaximos - 1);

if($productosMaximos <= 8){
    
    $productosMaximosFuncion = $productosMaximosResta;
}else{
    $productosMaximosFuncion = 7 ;
}

for ($i = 0; $i <= $productosMaximosFuncion; $i  ) {

echo $someArray['SearchResult']['Items'][$i]['ItemInfo']['Features']['Locale'];
}

Json code its from Amazon API 5, here are the all json that i can obtain.

{ 
 "SearchResult": {
  "Items": [
   {
    "ASIN": "0545162076",
    "DetailPageURL": "https://www.amazon.com/dp/0545162076?tag=dgfd&linkCode=osi",
    "Images": {
       /* Container for Images Resources if requested*/
    },
    "ItemInfo": {
       /* Container for ItemInfo Resources if requested */
    },
    "Offers": {
       /* Container for Offers Resources if requested */
    },
    {
       /* More items */
    }
   }
  ],
 }
}

For more information can find here: https://webservices.amazon.com/paapi5/documentation/search-items.html If you need more information please tell me and I give you all information that i know. Thank you for the help in advance.

CodePudding user response:

I just copied your JSON and typed up this code, it is echoing the locate as expected. There must be something strange about how you are building the limits productosMaximosFuncion, etc. Instead, you can use a foreach to look through the items and then use a break to jump out of the loop at the upper limit.

$json = json_decode($file, true);

foreach ($json['SearchResult']['Items'] as $i => $item) {
    if ($i > 7)
        break;
    foreach ($item['ItemInfo']['Features']['DisplayValues'] as $display_value) {
        echo $display_value . PHP_EOL;
    }
}

JSON I used:

{ 
"SearchResult": {
 "Items": [
  {
   "ASIN": "0545162076",
   "DetailPageURL": "https://www.amazon.com/dp/0545162076?tag=dgfd&linkCode=osi",
   "Images": {
   },
   "ItemInfo": {
    "Features": {
        "Label": "Features",
        "Locale": "en_US",
        "DisplayValues": [
            "Vented clear cover doubles as a 6-quart capacity popcorn bowl",
            "Stir rod is motorized and improves popping, get more popped corn, larger kernels per batch",
            "Convenient nesting lid is ideal for small storage"
        ]
    }
   },
   "Offers": {
   }
  }
 ]
}
}
  •  Tags:  
  • php
  • Related