dsa89029 2018-10-10 18:32
浏览 86
已采纳

找到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!

  • 写回答

1条回答 默认 最新

  • douxuanwei1980 2018-10-10 18:44
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line