【问题标题】:How to process JSON in PHP?如何在 PHP 中处理 JSON?
【发布时间】:2012-02-04 05:20:20
【问题描述】:

这是异步发送到我的 php 页面的 JSON。它本质上是一个产品列表,它将被插入到我的 mySQL 数据库中。

我的问题是在 PHP 中解码 JSON。我可以用 'eval' 函数在 js 中很好地做到这一点,但在 PHP 中,我的努力导致了一系列复杂的爆炸和内爆函数。

{
    "Product": [
        {
            "Product_Title": "Cloth",
            "Product_Description": "Here is cloth",
            "Price": "100",
            "Category_ID": "1"
        },
        {
            "Product_Title": "Cloth",
            "Product_Description": "Here is cloth",
            "Price": "100",
            "Category_ID": "1"
        },
        {
            "Product_Title": "Cloth",
            "Product_Description": "Here is cloth",
            "Price": "100",
            "Category_ID": "1"
        }
    ]
}

我知道 php 有一个内置的 json_decode 函数,但在 PHP 文档中它们只显示了如何处理数组。

非常感谢任何建议或帮助

泰勒

【问题讨论】:

标签: php json


【解决方案1】:

如果您拨打json_decode($data,true);,您的数据将是:

Array(
    "Product"=>Array(
        Array(
            "Product_Title"=>"Cloth",
            "Product_Description"=>"Here is cloth",
            "Price"=>"100",
            "Category_ID"=>"1"
        ),
        Array(
.............
        )
    )
);

这有什么问题?

【讨论】:

    【解决方案2】:

    如果要保留stdClass 对象,则需要使用对象属性语法。

    <?php
    
    $json = '{
        "Product": [
            {
                "Product_Title": "Cloth",
                "Product_Description": "Here is cloth",
                "Price": "100",
                "Category_ID": "1"
            },
            {
                "Product_Title": "Cloth",
                "Product_Description": "Here is cloth",
                "Price": "100",
                "Category_ID": "1"
            },
            {
                "Product_Title": "Cloth",
                "Product_Description": "Here is cloth",
                "Price": "100",
                "Category_ID": "1"
            }
        ]
    }
    ';
    
    $json_decoded = json_decode($json);
    
    // Note, it's usually a bad idea to use use count() like this;
    // cache the count before the for() in a variable and use that.
    // This is for demo purposes only. :)
    for ($i = 0; $i < count($json_decoded->{'Product'}); $i++) {
        echo "Products:
    " . $json_decoded->{'Product'}[$i]->{'Product_Title'} . "
    " . $json_decoded->{'Product'}[$i]->{'Product_Description'} . "
    " . $json_decoded->{'Product'}[$i]->{'Price'} . "
    " . $json_decoded->{'Product'}[$i]->{'Category_ID'} . "
    
    ";
    }
    
    ?>
    

    输出:

    Products:
    Cloth
    Here is cloth
    100
    1
    
    Products:
    Cloth
    Here is cloth
    100
    1
    
    Products:
    Cloth
    Here is cloth
    100
    1
    

    http://codepad.org/JxYAO5De

    【讨论】:

    • 大声笑,我可以指出讽刺的是:// Note, it's usually a bad idea to use use count() like this... 当实际以正确的方式进行操作时会更容易。我发现自己在编辑中经常这样做,因为人们愚蠢地根据为简单、简洁或演示目的而采用的快捷方式投反对票。
    • 或者......这是一个展示如何不这样做通常并主动不要这样做的机会.有些人会(通常)看到更好的方式,但仍然会退回到easy的方式。
    猜你喜欢
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多