从api中削减php中的json数组

时间:2016-06-11 21:55:47

标签: javascript php html angularjs

我从flipkart's API获得了一系列规范。

我能够从该API获得此响应:

{
    "nextUrl": "https://affiliate-api.flipkart.net/1.0/affiliate/feeds/fnkygygma/category/reh/55ab6c2673a4773ffb8e4019.json?expiresAt=1452881213871&sig=1c4c5111b6b014a71a17b229e6df6afc",
    "validTill": 1452881213871,
    "productInfoList": [
    {
        "productBaseInfoV1": {
        "productId": "TDHDMH5GRSPZ3DNM",
        "title": "Newhide Designer",
        "productDescription": "",
        "imageUrls": {
            "400x400": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-400x400-imadmh8zgxbrsgq6.jpeg",
            "200x200": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-200x200-imadmh8zgxbrsgq6.jpeg",
            "unknown": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-original-imadmh8zgxbrsgq6.jpeg",
            "800x800": "http://img.fkcdn.com/image/travel-document-holder/d/n/m/cdbp040739-newhide-passport-holder-designer-800x800-imadmh8zgxbrsgq6.jpeg"
        },
        "productFamily": null,
        "maximumRetailPrice": {
            "amount": 1145,
            "currency": "INR"
        },
        "flipkartSellingPrice": {
            "amount": 1145,
            "currency": "INR"
        },
        "flipkartSpecialPrice": {
            "amount": 1145,
            "currency": "INR"
        },
        "productUrl": "http://dl.flipkart.com/dl/newhide-designer/p/itmdp2nunbzffwzr?pid=TDHDMH5GRSPZ3DNM&affid=keshav",
        "productBrand": "Newhide",
        "inStock": true,
        "codAvailable": true,
        "discountPercentage": 0,
        "offers": [],
        "categoryPath": "[[{\"node_id\":20001,\"node_name\":\"FLIPKART_TREE\"},{\"node_id\":21183,\"node_name\":\"Lifestyle\"},{\"node_id\":21499,\"node_name\":\"Leather \\u0026 Travel Accessories\"},{\"node_id\":21960,\"node_name\":\"Wallets \\u0026 Clutches\"},{\"node_id\":21274,\"node_name\":\"Wallets \\u0026 Card Wallets\"}]]",
        "styleCode": null,
        "attributes": {
            "size": "",
            "color": "Black",
            "storage": "",
            "sizeUnit": "",
            "displaySize": ""
        }
      },
      "productShippingInfoV1": {
          "shippingCharges": {
              "amount": 0,
              "currency": "INR"
          },
          "sellerName": null,
          "sellerAverageRating": null,
          "sellerNoOfRatings": 0,
          "sellerNoOfReviews": 0
      },
      "categorySpecificInfoV1": {
          "keySpecs": [
              "Passport Holder",
              "Passport Wallet"
           ],
           "detailedSpecs": [],
           "specificationList": [
           {
            "key": "General",
            "values": [
              {
                "key": "Type",
                "value": [
                  "Passport Organizer"
                ]
              },
              {
                "key": "Ideal For",
                "value": [
                  "Men's, Women's"
                ]
              },
              {
                "key": "Material",
                "value": [
                  "Leather, Cloth"
                ]
              },
              {
                "key": "Slot for Cards",
                "value": [
                  "Yes"
                ]
              },
              {
                "key": "Slot for Passport",
                "value": [
                  "Yes"
                ]
              },
              {
                "key": "Slot for Cheque Book",
                "value": [
                  "Yes"
                ]
              },
              {
                "key": "Style Code",
                "value": [
                  "CDBP040739"
                ]
              },
              {
                "key": "Color Code",
                "value": [
                  "Black"
                ]
              }
            ]
          },
          {
            "key": "Warranty",
            "values": [
              {
                "key": "  ",
                "value": [
                  "1 Year Newhide warranty"
                ]
              }
            ]
          }
        ],
        "booksInfo": {
          "language": null,
          "binding": null,
          "pages": null,
          "publisher": null,
          "year": 0,
          "authors": []
        },
        "lifeStyleInfo": {
          "sleeve": null,
          "neck": null,
          "idealFor": [
            "Men's",
            "Women's"
          ]
        }
      }
    }
}

我无法解码

"categorySpecificInfoV1": {
  "keySpecs": [
    "Passport Holder",
    "Passport Wallet"
  ],

我试过了json_decode。这给出了错误

  

警告:json_decode()期望参数1为字符串,给定数组。

 $keySpecs=$product['categorySpecificInfoV1']['keySpecs'];
 $ar = json_decode($keySpecs,true); 

我是PHP的新手,如何从keyspecs数组中打印keyspecs?

1 个答案:

答案 0 :(得分:1)

警告很清楚。您需要将从Flipkart获得的整个字符串传递给json_decode()函数。

See Documentation

$ar = json_decode($flipkart_result, true);  
print_r($ar); //to check the array.
print_r($ar['categorySpecificInfoV1']['keySpecs']);

这应该做。如果这没有帮助,请发布更多信息。