javascript - 从 api 中解析 php 中的 json 数组

标签 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。这会产生错误

Warning: json_decode() expects parameter 1 to be string, array given.

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

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

最佳答案

警告非常明确。您需要将从 Flipkart 获取的整个字符串传递到 json_decode() 函数中。

See Documentation

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

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

关于javascript - 从 api 中解析 php 中的 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37768838/

相关文章:

php - HTML 表单不能很好地处理表单输入

html - 需要在html中发送 bool 值

Javascript 函数语法错误输出

javascript - 如何将参数的字符串列表传递给函数并将它们分配为单独的参数

php - 语法错误,意外 'endforeach'

php - 小数点后0位四舍五入

javascript - 如何确定下拉菜单应显示向上还是向下方向

javascript - Box Node SDK 未使用 readStream 拉取文件

javascript - 仅获取选中复选框的那些输入的标签名称

php - 为什么查询成功后我会从 mysqli_result::free 收到 fatal error ?