json_encode returns null on valid JSON string
P粉176980522
P粉176980522 2023-07-28 19:35:06
0
1
492
<p>I'm using PHP's json_decode function to parse a JSON string created by JavaScript's JSON.stringify; </p><p>JSON appears to be valid: </p><p>JSON String: </p><p><br /></p> <pre class="brush:php;toolbar:false;">"{"m_user_name":"test","m_user_phone":" 60 12-384 7961","m_user_city":"Kuala Lumpur","m_user_linkedin ":"example@gmail.com","m_user_g_plus":"example@gmail.com"}"</pre> <p>JSON final error message: "Syntax error"</p><p>Okay, if I paste the string directly into json_decode it works. I'm sending a string via ajax via a form_data object and then trying to decode the post variable. </p><p>The following is the code for the ajax request:</p><p><br /></p> <pre class="brush:php;toolbar:false;">$.ajax({ type: "POST", url: ajaxurl, data: formdata, cache: false, contentType: false, processData: false, beforeSend: function () { toggle_loading_spinner(); }, success: function (data) { console.log(data); toggle_loading_spinner(); }, });</pre> <p>I tried using preg_replace('/[x00-x1Fx80-xFF]/', '', $JSON_STRING) to remove the unwanted symbols, but I got the same error. </p>
P粉176980522
P粉176980522

reply all(1)
P粉814160988

json_decode() function only works with strings encoded in UTF-8. The input may be in a different encoding. After removing the unwanted characters, try converting the encoding to UTF-8. Here are references on how to do the conversion:

https://www.php.net/manual/en/function.mb-convert-encoding.php

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!