Json_decode在WordPress上不起作用

时间:2013-03-06 作者:soul

我试图在wordpress中将json字符串转换回数组。下面是我从数据库返回的json字符串:

[
    {
        "item_title": "Kindle Fire HD 7\\", Dolby Audio, Dual-Band Wi-Fi, 16 GB - Includes Special Offers",
        "item_url": "http://my.dev/tester/wordpress/wp-content/ecom-plugin-redirects/ecom_redirector.php?id=2342",
        "large_image": "http://my.dev/tester/wordpress/wp-content/uploads/2013/03/29_51EN7cswSYL.jpg",
        "formatted_price": "$199.00",
        "stars_url": "http://my.dev/tester/wordpress/wp-content/plugins/ecom/img/stars-4-0.gif",
        "product_review": "World\\\'s most advanced 7\\" tablet with stunning HD display, exclusive Dolby audio, and the fastest Wi-Fi found on a tablet. When it comes to HD displays, great resolution is just the start. Kindle Fire HD delivers rich color and deep contrast from every angle, with an advanced polarizing filter and custom anti-glare technology. Our exclusive  Dolby Digital Plus audio  includes technology to adjust volume, create virtual surround sound, and deliver easier-to-understand dialogue in movies and TV shows.",
        "review_rating": "4 out of 5",
        "reviews_url": "http://my.dev/tester/wordpress/wp-content/ecom-plugin-redirects/ecom_redirector.php?id=2347",
        "reviews_label": "Read all 9,972 customer reviews",
        "create_review_url": "http://my.dev/tester/wordpress/wp-content/ecom-plugin-redirects/ecom_redirector.php?id=2346",
        "create_review_label": "Write a review",
        "form_id": "6653b8329db2abc61eba9f32c26d1c4c",
        "add_to_cart_action": "http://my.dev/tester/wordpress/wp-content/ecom-plugin-redirects/add_to_cart.php",
        "asin": "B0083PWAPW",
        "total_items": "",
        "item_status": "In Stock",
        "customer_images": [
            "http://my.dev/tester/wordpress/wp-content/uploads/2013/03/29_61hMQEAJpRL.jpg"
        ],
        "disclaimer": "",
        "item_attributes": [
            {
                "attr": "Binding",
                "value": "Electronics"
            },
            {
                "attr": "Brand",
                "value": "Amazon Digital Services Inc."
            },
            {
                "attr": "Color",
                "value": "Black"
            },
            {
                "attr": "EAN",
                "value": "2609000005244"
            },
            {
                "attr": "Label",
                "value": "Amazon Digital Services, Inc"
            },
            {
                "attr": "Manufacturer",
                "value": "Amazon Digital Services, Inc"
            },
            {
                "attr": "Model",
                "value": "53-000406"
            },
            {
                "attr": "MPN",
                "value": "0379"
            },
            {
                "attr": "PackageQuantity",
                "value": "1"
            },
            {
                "attr": "PartNumber",
                "value": "0379"
            },
            {
                "attr": "ProductGroup",
                "value": "Amazon Devices"
            },
            {
                "attr": "ProductTypeName",
                "value": "ABIS_ELECTRONICS"
            },
            {
                "attr": "Publisher",
                "value": "Amazon Digital Services, Inc"
            },
            {
                "attr": "ReleaseDate",
                "value": "2012-09-14"
            },
            {
                "attr": "Studio",
                "value": "Amazon Digital Services, Inc"
            },
            {
                "attr": "Title",
                "value": "Kindle Fire HD 7\\", Dolby Audio, Dual-Band Wi-Fi, 16 GB - Includes Special Offers"
            },
            {
                "attr": "UPC",
                "value": "848719003796"
            }
        ],
        "customer_review_url": "http://my.dev/tester/wordpress/wp-content/ecom-customer-reviews/61b553856a18b530399c2158ebbfc71d.html",
        "flickr_results": [
            "http://my.dev/tester/wordpress/wp-content/uploads/2013/03/29_8418129172_d9cd0358ce_m.jpg"
        ],
        "freebase_text": "Amazon.com, Inc. (NASDAQ:u00a0AMZN) is an American multinational electronic commerce company with headquarters in Seattle, Washington, United States. It is the world\\\'s largest online retailer. The company also produces consumer electronics - notably the Amazon Kindle e-book reader - and is a major provider of cloud computing services.\\\\nAmazon has separate retail websites for the following countries: United States, Canada, United Kingdom, France, Germany, Italy, Spain, Japan, and China, with international shipping to certain other countries for some of its products. It is also expected to launch its websites in Poland, Netherlands and Sweden.\\\\nJeff Bezos incorporated the company (as Cadabra) in July 1994, and the site went online as amazon.com in 1995. The company was renamed after the Amazon River, one of the largest rivers in the world, which in turn was named after Amazons, the legendary nation of female warriors in Greek mythology. Amazon.com started as an online bookstore, but soon diversified, selling DVDs, CDs, MP3 downloads, software, video games, electronics, apparel, furniture, food, toys, and jewelry.\\\\nThe company was founded in 1994, spurred by what Bezos called his \\"regret",
        "freebase_image": "http://my.dev/tester/wordpress/wp-content/uploads/2013/03/0mgkg?maxwidth=150&maxheight=150&key=AIzaSyBU4RGRI2UlcVoS7fEAWhKBURGp7-DCb1A",
        "ebay_related_items": [
            {
                "title": "Amazon Kindle Fire HD 16GB, Dual-Band WiFi, 7in, Dolby Audio-Black(Latest Model)",
                "image": "http://my.dev/tester/wordpress/wp-content/uploads/2013/03/200_140.jpg",
                "url": "http://my.dev/tester/wordpress/wp-content/ecom-plugin-redirects/ecom_redirector.php?id=3946",
                "currency_id": "USD",
                "current_price": "215.0"
            },
            {
                "title": "Kindle Fire HD 7\\" Dolby Audio Dual-Band Wi-Fi 16GB w/ Special Offers from Amazon",
                "image": "http://my.dev/tester/wordpress/wp-content/uploads/2013/03/201_140.jpg",
                "url": "http://my.dev/tester/wordpress/wp-content/ecom-plugin-redirects/ecom_redirector.php?id=3966",
                "currency_id": "USD",
                "current_price": "170.0"
            }
        ]
    }
]
当我使用json_decode($json, true) 当我不在wordpress的时候。它正确地转换为数组,没有任何问题。

但在wordpress中,为了获得正确的json字符串,我必须执行以下操作:

$valid_json = str_replace(\'\\\\\\n\', \'\\n\', $json);
$data = json_decode($valid_json, true);
存储在中的值$valid_json 在wordpress之外工作非常完美,但在wordpress内部我可以NULL 回到我身边。

我试过使用var_dump() 检查我是否得到了完全相同的结果:

$post_id = get_the_ID();
$results_cache = get_results($post_id);
$template_name = $results_cache->template_name;
$json = trim($results_cache->result);
$json = str_replace(\'\\\\\\n\', \'\\n\', $json);

var_dump($json); 
    var_dump($json_str);
我得到的第一个var\\u垃圾场string(9841). 第二个是我直接从数据库中复制的字符串string(9839)

我注意到的另一件事是,当我直接复制存储在数据库中的内容并使用json_decode() 在wordpress内部,无需使用:

str_replace(\'\\\\\\n\', \'\\n\', $json);
我用来从数据库中提取数据的函数使用$wpdb

function get_results($post_id){

    global $wpdb;

    $post = $wpdb->get_row("
        SELECT result, template_name 
        FROM results 
        WHERE post_id = \'$post_id\'
    ");

    return $post;
}
我不知道使用查询从数据库获取数据和直接复制数据库上的数据有什么区别。

我还需要做些什么才能让它起作用吗?

2 个回复
最合适的回答,由SO网友:RRikesh 整理而成

我试图将您的JSON解析为this 出现错误,您的转义字符不正确(必须转义反斜杠)。

考虑到Vinod Dalvi\'s comment, 定义json_encode 在WordPress中,由function_exists(). 我想如果你有5.2或更高版本的PHP(我想你有,因为你可以使用json_decode 在WordPress之外),定义的WordPressjson_decode() 不会被使用。

SO网友:Mark Kaplun

Wordpress在$\\u GET和$\\u POST上转义斜杠,将“\\”变为“\\”。如果您通过ajax传递json,那么这就是您得到额外斜杠的原因。在将值存储到DB中或使用它之前,可能应该删除斜杠。

Discussion at the wordpress.org support forum

Beware. this behavior will most likely be removed in version 3.6

结束

相关推荐

如何替换或显示JSON API插件中的特殊字符

我开始使用JSON API插件。它工作得很好!但我面临的问题是,如果我通过CMS输入任何包含特殊字符的文本,输出结果与给定的文本不一样。为什么会这样?示例:这是一个很好的应用程序(输入),输出看起来像';这是一个很好的应用程序。请提出任何摆脱这种情况的提示/方法。急切地等待你的回应。非常感谢。