我有一个MySQL查询,以便从数据库中获取一些记录。
然后,在循环中,我有了一个PHP数组,最后有了json_encode()
PHP函数。我从数据库获取的值具有一些HTML标记,因此json_encode()
不起作用。
请查看示例代码:
$pr_arr['products'][] = array(
"id" => $results_products->id,
"business_id" => $results_products->business_id,
"pid" => $results_products->pid,
"category" => $results_products->category,
"style_mapping" => $results_products->style_mapping,
"name" => $product_name,
"price" => $price,
"offer_price" => $offer_price,
"product_link" => $product_link,
"image_url" => $product_image,
"size" => $results_products->size,
"manufacturer" => $product_manufacturer,
"description" => $product_description,
"color" => $results_products->color,
"sku" => $results_products->sku,
"mpn" => $results_products->mpn,
"instock" => $results_products->instock,
"availability" => $results_products->availability,
"date_added" => $results_products->date_added
);
$product_description
是有问题的那个。如果我注释此行代码,则JSON正确有效!
我从https://jsonlint.com/中看到的验证(如果我在浏览器中查看,则是这样):
"manufacturer":"combos","description":"
\\\\n\tv-\u03bb\u03b1\u03b9\u03bc\u03cc\u03ba\u03bf\u03c8\u03b7<\/li>
\\\\n\t\u03c0\u03b1\u03c4\u03b9\u03bb\u03ad\u03c4\u03b1 \u03bc\u03b5 \u03ba\u03bf\u03c5\u03bc\u03c0\u03b9\u03ac<\/li>
\\\\n\t\u03bc\u03b1\u03bd\u03c3\u03ad\u03c4\u03b5\u03c2 \u03ba\u03b1\u03b9 \u03c3\u03c4\u03c1\u03af\u03c6\u03c9\u03bc\u03b1 \u03c3\u03b5 rib \u03c0\u03bb\u03ad\u03be\u03b7<\/li>
regular fit<\/li>
我读到,当您打开双引号时,我需要将所有内容放在同一行中。但是没有,我已经尝试过了。我尝试用空格替换新行,转义特殊字符(\ n,\ r,\ t等)
有人可以告诉我如何解决此问题,以便我可以在JSON内帮助使用HTML标签进行描述吗?
提前谢谢!
答案 0 :(得分:1)
尝试在您的json_encode
命令中添加标志
json_encode($yourArrayVar, JSON_HEX_QUOT | JSON_HEX_TAG);
要么
json_encode($yourArrayVar, JSON_HEX_APOS | JSON_HEX_TAG);
还要尝试将其与Anis Hikmat Abu-hmiad标志混合使用