-1

我有一些解析 JSON 的 Javascript 代码,我想获取 o.products 和 cp.key 上的“options”和“childProducts”数据。有没有办法让我在没有无休止的字符串解析的情况下有效地做到这一点?

var spConfig = new Product.Config(
{"attributes":{
    "451":{
        "id":"451",
        "code":"package_size",
        "label":"Package size",
        "options":[
            {"id":"4",
            "label":"1 lb.",
            "oldPrice":"0",
            "products":["4562"],
            "brewsaver":"1"},
            {"id":"62",
            "label":"1 lb. crushed",
            "oldPrice":"0",
            "products":["4649"],
            "brewsaver":"1"},
            {"id":"14",
            "label":"55 lbs.",
            "oldPrice":"0",
            "products":["704"],
            "brewsaver":null}
            ]
        }
        },
    "template":"$#{price}", 
    "basePrice":"1.75",
    "oldPrice":"1.75",
    "productId":"3390",
    "chooseText":"Choose an Option...",
    "taxConfig":        {
        "includeTax":false,
        "showIncludeTax":false,
        "showBothPrices":false,
        "defaultTax":0,
        "currentTax":0,
        "inclTaxTitle":"Incl. Tax"
        },
    "childProducts":
        {
        "704":
            {
            "price":"64.99",
            "finalPrice":"64.99"
            },
        "4562":
            {
            "price":"1.75",
            "finalPrice":"1.75"
            },
        "4649":
            {
            "price":"1.75",
            "finalPrice":"1.75"
            }
        },
4

1 回答 1

-1

以 JSON开头的部分{"attributes"看起来像有效的JSON,因此如果您将其隔离(var spConfig = new Product.Config(从开头删除,);我假设在结尾),您可以使用json_decode获取一组嵌套的 PHP 关联数组。

然后就是旋转这些数组并将它们连接起来,不需要进行字符串解析。

于 2013-08-21T04:54:59.293 回答