首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转换为Array的JSON答复在PHP中打印时没有显示输出

转换为Array的JSON答复在PHP中打印时没有显示输出
EN

Stack Overflow用户
提问于 2016-04-04 12:36:37
回答 5查看 86关注 0票数 0

这是我使用Flipkart的API进行查询时得到的JSON答复。我想从我正在设计的搜索列表页面中的API回复中打印productId和标题。

代码语言:javascript
复制
{
   "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
      }
   ]
}

这是我用来打印Array中元素的php代码。但我似乎没有得到任何输出。

$response是我从API中得到的答复。

代码语言:javascript
复制
$array = json_decode($response, true);
$test1 = $array['productAttributes']['title'];
$test2 = $array['productInfoList']['productBaseInfo']['productIdentifier']['productId'];

    echo $test1;
    echo test2;

调试-

代码语言:javascript
复制
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释 
* 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] => ) ) )
*/
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2016-04-04 12:51:46

首先循环数组值(从json解码),然后相应地获取所需的数据,执行如下操作-

代码语言:javascript
复制
$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
EN

Stack Overflow用户

发布于 2016-04-04 12:41:54

仔细查看JSON,您将看到productInfoList是一个具有名为productBaseInfo的属性的对象,其值是对象的数组(例如,[....],尽管它只有一个条目),每个条目都具有类似于productBaseInfo的属性。

因此,要查看第一个(仅)条目的productBaseInfo.productIdentifier.productId,您需要:

代码语言:javascript
复制
$test2 = $array['productInfoList'][0]['productBaseInfo']['productIdentifier']['productId'];
// Note --------------------------^^^
票数 2
EN

Stack Overflow用户

发布于 2016-04-04 12:52:40

删除所有单引号。尝尝这个。

代码语言:javascript
复制
    <?php
    $array = json_decode($v, true);   
    <pre>   
    print_r($array['productInfoList'][0]['productBaseInfo']['productIdentifier']);    
 print_r($array['productInfoList'][0]['productBaseInfo']['productAttributes']['title']);
    ?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36402863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档