我试图在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;
}
我不知道使用查询从数据库获取数据和直接复制数据库上的数据有什么区别。
我还需要做些什么才能让它起作用吗?