2016-04-04 2 views
0

Dies ist die JSON-Antwort, die ich für eine Abfrage bekomme ich mit einer API für Flipkart. Ich wollte die Produkt-ID und den Titel aus der API-Antwort in der Suchseite drucken, die ich entwerfe.JSON-Antwort in Array konvertiert zeigt keine Ausgabe, wenn in PHP gedruckt

{ 
    "productInfoList":[ 
     { 
     "productBaseInfo":{ 
      "productIdentifier":{ 
       "productId":"MOBE89M7BPKYHZGX", 
       "categoryPaths":{ 
        "categoryPath":[ 
        [ 
         { 
          "title":"Mobiles>Handsets" 
         } 
        ] 
        ] 
       } 
      }, 
      "productAttributes":{ 
       "title":"Sony Xperia C4 Dual", 
       "productDescription":"Are you a selfie fan? If yes, then grab this exceptional Sony Xperia C4 Dual smartphone which has been designed to click the best PROselfies. Apart from an amazing front and rear camera, this Full HD smartphone offers maximum performance and speed. All You Need For PROselfies For all selfie lovers, the 5 MP front camera of this Sony device is equipped with a soft LED flash, 25 mm wide-angle lens and other features to click the best PROselfies. Exmor R sensor ensures sharp pictures with low noise. HDR balances bright background lights for clearer results. SteadyShot lets you shoot amazing selfie videos. Unique Soft LED Flash The unique soft LED flash automatically adjusts according to the lighting conditions, illuminates the scene and gives you the perfect shot. Whether there is a bright backlight or you are taking photos at night, the Superior Auto feature ensures that your selfies and group selfies are of professional quality. 25 mm Wide View Thanks to the 25 mm wide-angle lens with an 80-degree field of view, you can fit in more of your friends or more of the view in a single shot. Main Camera This Sony Xperia smartphone is equipped with a 13 MP primary shooter. Exmor RS For Mobile This image sensor technology assures sharp photos and videos with fine detailing, amazing picture clarity and rich colors, even while shooting in challenging light conditions. Superior Auto The Superior Auto feature recognises up to 52 different scenarios such as illuminated night shots and close-up macros. This intelligent feature adjusts the settings according to the scene, so you get noteworthy photos always. HDR For Photo & Video HDR mode shoots the same image multiple times under different exposures and layers them, so that you get clear and bright photos and videos. This feature is extremely useful while capturing photos or videos in strong backlight or high-contrast situations. SteadyShot Thanks to SteadyShot, every video you shoot with this Xperia smartphone is smooth, steady and perfect. Sound Photo Relive you favourite moments with sound and visuals. Capture up to 10 seconds of audio with your photos using Sound Photo app and share it via PlayMemories Online. AR Mask Replace your own face or other people's faces with a different face using the AR Mask app. You can choose from the pre-loaded faces or create your own collection. Style Portrait This app lets you select from different styles to enhance your photos or add fun effects to them. These effects can be applied in real-time or after taking the photo or video. Download More Apps Apart from the pre-installed apps, you can download more camera apps directly from the camera interface of this Xperia smartphone. Display The large 13.97 cm (5.5) Full HD display and Mobile Bravia Engine 2 make the Xperia C4 an ideal device for watching movies and playing games. The IPS technology allows excellent viewing from all angles, so you and your friends can enjoy a movie on this device while chilling out in the college cafeteria. Sound Equipped with a speaker capable of up to 97 decibels and ClearAudio+ technologies for crystal clear audio, you can pump up your home get-togethers with loud music. This Xperia device delivers rich bass, thanks to the xLoud loudness enhancement engine. Design Despite the large screen size, the Xperia C4 is sleek, lightweight and easily slides into your jeans pocket or wallet. The device is 7.9 mm thin and weighs just 147 grams. Processor Play games lag-free, run multiple apps simultaneously, surf the web seamlessly and stream HD videos without glitches. The competent 1.7 GHz Octa Core processor and 2 GB RAM offers maximum performance in everything you do on your Xperia smartphone. The phone also comes with a built-in LTE/4G modem for extremely fast internet connectivity. Battery An incredibly powerful yet efficient 2600 mAh battery powers this Xperia phone. To further extend the already long battery life, this device offers four power management modes - Battery STAMINA mode, Extended standby mode, Extended usage mode and Ultra STAMINA mode.", 
       "imageUrls":{ 
        "400x400":"http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-400x400-imae8dsymwzxb6u2.jpeg", 
        "200x200":"http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-200x200-imae8dsymwzxb6u2.jpeg", 
        "unknown":"http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-original-imae8dsymwzxb6u2.jpeg", 
        "800x800":"http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-800x800-imae8dsymwzxb6u2.jpeg" 
       }, 
       "maximumRetailPrice":{ 
        "amount":29490.0, 
        "currency":"INR" 
       }, 
       "sellingPrice":{ 
        "amount":19699.0, 
        "currency":"INR" 
       }, 
       "productUrl":"http://dl.flipkart.com/dl/sony-xperia-c4-dual/p/itme9gw2xxxgd9wy?pid=MOBE89M7BPKYHZGX&affid=admintechz", 
       "productBrand":"Sony", 
       "inStock":true, 
       "isAvailable":true, 
       "codAvailable":true, 
       "emiAvailable":null, 
       "discountPercentage":33.0, 
       "cashBack":null, 
       "offers":[ 

       ], 
       "size":null, 
       "color":"Black", 
       "sizeUnit":"", 
       "sizeVariants":"[MOBE89M8BXKKYMBP, MOBE89M8Y4RN9NHT, MOBE89M7BPKYHZGX]", 
       "colorVariants":"[MOBE89M8BXKKYMBP, MOBE89M8Y4RN9NHT]", 
       "styleCode":null 
      } 
     }, 
     "productShippingBaseInfo":{ 
      "shippingOptions":null 
     }, 
     "offset":null 
     } 
    ] 
} 

Dies ist der PHP-Code, den ich verwende, um die Elemente im Array zu drucken. Aber ich bekomme keine Ausgabe.

$ Antwort ist die Antwort, die ich von der API bekomme.

$array = json_decode($response, true); 
$test1 = $array['productAttributes']['title']; 
$test2 = $array['productInfoList']['productBaseInfo']['productIdentifier']['productId']; 

    echo $test1; 
    echo test2; 

debug -

Array ([productInfoList] => Array ([0] => Array ([productBaseInfo] => Array ([productIdentifier] => Array ([productId] => MOBE89M7BPKYHZGX [categoryPaths] => Array ([categoryPath] => Array ([0] => Array ([0] => Array ([title] => Mobiles>Handsets))))) [productAttributes] => Array ([title] => Sony Xperia C4 Dual [productDescription] => Are you a selfie fan? If yes, then grab this exceptional Sony Xperia C4 Dual smartphone which has been designed to click the best PROselfies. Apart from an amazing front and rear camera, this Full HD smartphone offers maximum performance and speed. All You Need For PROselfies For all selfie lovers, the 5 MP front camera of this Sony device is equipped with a soft LED flash, 25 mm wide-angle lens and other features to click the best PROselfies. Exmor R sensor ensures sharp pictures with low noise. HDR balances bright background lights for clearer results. SteadyShot lets you shoot amazing selfie videos. Unique Soft LED Flash The unique soft LED flash automatically adjusts according to the lighting conditions, illuminates the scene and gives you the perfect shot. Whether there is a bright backlight or you are taking photos at night, the Superior Auto feature ensures that your selfies and group selfies are of professional quality. 25 mm Wide View Thanks to the 25 mm wide-angle lens with an 80-degree field of view, you can fit in more of your friends or more of the view in a single shot. Main Camera This Sony Xperia smartphone is equipped with a 13 MP primary shooter. Exmor RS For Mobile This image sensor technology assures sharp photos and videos with fine detailing, amazing picture clarity and rich colors, even while shooting in challenging light conditions. Superior Auto The Superior Auto feature recognises up to 52 different scenarios such as illuminated night shots and close-up macros. This intelligent feature adjusts the settings according to the scene, so you get noteworthy photos always. HDR For Photo & Video HDR mode shoots the same image multiple times under different exposures and layers them, so that you get clear and bright photos and videos. This feature is extremely useful while capturing photos or videos in strong backlight or high-contrast situations. SteadyShot Thanks to SteadyShot, every video you shoot with this Xperia smartphone is smooth, steady and perfect. Sound Photo Relive you favourite moments with sound and visuals. Capture up to 10 seconds of audio with your photos using Sound Photo app and share it via PlayMemories Online. AR Mask Replace your own face or other people's faces with a different face using the AR Mask app. You can choose from the pre-loaded faces or create your own collection. Style Portrait This app lets you select from different styles to enhance your photos or add fun effects to them. These effects can be applied in real-time or after taking the photo or video. Download More Apps Apart from the pre-installed apps, you can download more camera apps directly from the camera interface of this Xperia smartphone. Display The large 13.97 cm (5.5) Full HD display and Mobile Bravia Engine 2 make the Xperia C4 an ideal device for watching movies and playing games. The IPS technology allows excellent viewing from all angles, so you and your friends can enjoy a movie on this device while chilling out in the college cafeteria. Sound Equipped with a speaker capable of up to 97 decibels and ClearAudio+ technologies for crystal clear audio, you can pump up your home get-togethers with loud music. This Xperia device delivers rich bass, thanks to the xLoud loudness enhancement engine. Design Despite the large screen size, the Xperia C4 is sleek, lightweight and easily slides into your jeans pocket or wallet. The device is 7.9 mm thin and weighs just 147 grams. Processor Play games lag-free, run multiple apps simultaneously, surf the web seamlessly and stream HD videos without glitches. The competent 1.7 GHz Octa Core processor and 2 GB RAM offers maximum performance in everything you do on your Xperia smartphone. The phone also comes with a built-in LTE/4G modem for extremely fast internet connectivity. Battery An incredibly powerful yet efficient 2600 mAh battery powers this Xperia phone. To further extend the already long battery life, this device offers four power management modes - Battery STAMINA mode, Extended standby mode, Extended usage mode and Ultra STAMINA mode. [imageUrls] => Array ([400x400] => http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-400x400-imae8dsymwzxb6u2.jpeg [200x200] => http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-200x200-imae8dsymwzxb6u2.jpeg [unknown] => http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-original-imae8dsymwzxb6u2.jpeg [800x800] => http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-800x800-imae8dsymwzxb6u2.jpeg) [maximumRetailPrice] => Array ([amount] => 29490 [currency] => INR) [sellingPrice] => Array ([amount] => 19699 [currency] => INR) [productUrl] => http://dl.flipkart.com/dl/sony-xperia-c4-dual/p/itme9gw2xxxgd9wy?pid=MOBE89M7BPKYHZGX&affid=admintechz [productBrand] => Sony [inStock] => 1 [isAvailable] => 1 [codAvailable] => 1 [emiAvailable] => [discountPercentage] => 33 [cashBack] => [offers] => Array () [size] => [color] => Black [sizeUnit] => [sizeVariants] => [MOBE89M8BXKKYMBP, MOBE89M8Y4RN9NHT, MOBE89M7BPKYHZGX] [colorVariants] => [MOBE89M8BXKKYMBP, MOBE89M8Y4RN9NHT] [styleCode] =>)) [productShippingBaseInfo] => Array ([shippingOptions] =>) [offset] =>))) 
+3

Debug Ihr Array sollte es sein '$ test1 = $ array [ 'productInfoList'] [0] [ 'productBaseInfo'] [ 'productAttributes'] [ 'title']; $ test2 = $ array ['productInfoList'] [0] ['productBaseInfo'] ['productIdentifier'] ['productId']; ' –

Antwort

2

Sie sollten zuerst eine Schleife durch die Array-Werte (von json decodiert) und holen dann die benötigten Daten entsprechend, tun dies so etwas wie -

$array = json_decode($r, true); 
// print_r($array); //debug 
foreach($array['productInfoList'] as $product){ 
    echo $product['productBaseInfo']['productAttributes']['title']; 
    echo $product['productBaseInfo']['productIdentifier']['productId']; 
} 
2

Schauen Sie genau auf die JSON, und Sie werden sehen, dass productInfoList ist ein Objekt mit einer Eigenschaft namens productBaseInfo, deren Wert ein Array von Objekten (zB [....] , obwohl es nur einen Eintrag hat), von denen jeder Eigenschaften wie productBaseInfo hat.

So ist die erste zu sehen (nur) Eintritt productBaseInfo.productIdentifier.productId, würden Sie wollen:

$test2 = $array['productInfoList'][0]['productBaseInfo']['productIdentifier']['productId']; 
// Note --------------------------^^^ 
0

Verwenden Sie den folgenden Code ein. Es gibt Ihnen Titel und ProductID $ array = json_decode ($ string, true);

$ test1 = $ array ['productInfoList'] [0] ['productBaseInfo'] ['productAttributes'] ['title']; $ test2 = $ array ['productInfoList'] [0] ['productBaseInfo'] ['productIdentifier'] ['productId'];

0

In JSON {bezeichnet und Object und [bezeichnet ein Array. Um alle Attribute können Sie so etwas wie folgt verwenden:

$array = json_decode($response, true); 
$productInfoList_Array = $array['productInfoList']; 
foreach($productInfoList_Array as $productInfoList){ 
    $productBaseInfo = $productInfoList['productBaseInfo']; 
    $productAttributes = $productBaseInfo['productAttributes']; 
    foreach($productAttributes as $Attribute_Key => $Attribute_Value){ 
     if (!is_array($Attribute_Value)){ 
      //Attribute Value is not an array. 
      echo "<b>{$Attribute_Key}</b>: {$Attribute_Value}"; 
      echo "<br>"; 
     }else{ 
      //Attribute Value is a sub array 
      foreach($Attribute_Value as $Attribute_Sub_Key => $Attribute_Sub_Value){ 
       echo "<b>Sub Key: {$Attribute_Sub_Key}</b>: {$Attribute_Sub_Value}"; 
       echo "<br>";     
      } 
     } 
    } 
    echo "<hr><b>End of Product.</b>"; 
} 

Sehen Sie, dass productAttributes auch Subarray hat.

1

alle Apostroph entfernen. Versuchen Sie dies.

<?php 
    $array = json_decode($v, true); 
    <pre> 
    print_r($array['productInfoList'][0]['productBaseInfo']['productIdentifier']);  
print_r($array['productInfoList'][0]['productBaseInfo']['productAttributes']['title']); 
    ?> 
+0

Dies ist die erforderliche Ausgabe. – Karthik