找到JSON无效字符

找到JSON无效字符

问题描述:

I have a MySQL query in order to get some records from a DB. Then, inside a loop I have a PHP array and in the end I have json_encode() PHP function. The values I am getting from the DB, has some HTML tags, thus, the json_encode() does not work.

Please take a look at the sample code:

$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
            );

The $product_description is the one that has the issue. If I comment this line of code, the JSON in valid and correct!

The validation I am seeing from the https://jsonlint.com/ (and if I view it in browser is this):

"manufacturer":"combos","description":"
\\\
\tv-\u03bb\u03b1\u03b9\u03bc\u03cc\u03ba\u03bf\u03c8\u03b7<\/li>
\\\
\t\u03c0\u03b1\u03c4\u03b9\u03bb\u03ad\u03c4\u03b1 \u03bc\u03b5 \u03ba\u03bf\u03c5\u03bc\u03c0\u03b9\u03ac<\/li>
\\\
\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>

I read, that when you are opening the double quotes I need to have everything in the same row. But nothing, I have already tried worked. I tried to replace the new lines with empty spaces, escape special characters ( , , \t, and so on)

Could someone tell me how to solve this issue, so I could help the description (with HTML tags), inside my JSON?

thanks in advance!

try to add flags in your json_encode command json_encode($yourArrayVar, JSON_HEX_QUOT | JSON_HEX_TAG); or json_encode($yourArrayVar, JSON_HEX_APOS | JSON_HEX_TAG); Also try to mix it with Anis Hikmat Abu-hmiad flag