Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php JSON API失败,带“0”;变量中的(双引号)_Php_Json - Fatal编程技术网

Php JSON API失败,带“0”;变量中的(双引号)

Php JSON API失败,带“0”;变量中的(双引号),php,json,Php,Json,当“描述”字段有“(双引号)API失败时 用于检查用户发送的所有参数的图像 下面是从发布的JSON获取数据的代码 //Clear newline and tab from the jason sent from opti $cleanJSON = trim(preg_replace('/\s+/', ' ',file_get_contents("php://input"))); $jason_value = json_decode($cleanJSON);

当“描述”字段有“(双引号)API失败时

用于检查用户发送的所有参数的图像

下面是从发布的JSON获取数据的代码

    //Clear newline and tab from the jason sent from opti
     $cleanJSON = trim(preg_replace('/\s+/', ' ',file_get_contents("php://input")));
     $jason_value = json_decode($cleanJSON);
     $crm_id = $jason_value->data->crmId;
     $descriptions = $jason_value->data->descriptions;
我想删除“从中间的任何变量”,

descriptions=“我必须在下午2点打电话,”

我无法访问用户在其中输入说明的程序,我可以在其中添加“不允许验证”\n

转换后得到低于字符串的值

{ "jwt": "eyJ0", "data": { "crmId": "15876047", "geoconceptAppointmentId": "15876","geoconceptCustomerId": "15876047","status": "Rejected","appointmentDateTime": "","firstName": "Nick Test","lastName": "PA","address": "9112 RUE Tom","city": "MONTREAL","state": "QC","zip": "H2N1T1","country": "CAN","phoneNumber1": "5148332222","phoneNumber2": "5148332222","email": "nbskgg@gmail.com","dateEntry": "2019-06-20 12:02","dateModify": "2019-06-20 12:02","preferredWayToContact": "","textMsgFlag": "Y","hearAboutUs": "Referral","perferredTime": "Anytime","descriptions": "I have to call at 2" pm","worklog": "This is the comment ","rejectReason": "Area | Region","referredByDC": "09999","referredByStoreUsername": "store215","assignedUsername": "","createdByUsername": "np","modifiedByUsername": "np","btgMarket": "Montreal"}}


它可能会从php.net帮助您

<?php
$a = array('<foo>',"'bar'",'"baz"','&blong&', "\xc3\xa9");

echo "Normal: ",  json_encode($a), "\n";
echo "Tags: ",    json_encode($a, JSON_HEX_TAG), "\n";
echo "Apos: ",    json_encode($a, JSON_HEX_APOS), "\n";
echo "Quot: ",    json_encode($a, JSON_HEX_QUOT), "\n";
echo "Amp: ",     json_encode($a, JSON_HEX_AMP), "\n";
echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
echo "All: ",     json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n";

$b = array();

echo "Empty array output as array: ", json_encode($b), "\n";
echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "\n\n";

$c = array(array(1,2,3));

echo "Non-associative array output as array: ", json_encode($c), "\n";
echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "\n\n";

$d = array('foo' => 'bar', 'baz' => 'long');

echo "Associative array always output as object: ", json_encode($d), "\n";
echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n";


这里有一个正则表达式,它试图检测未转义的引号

(?<![ ,\\])"(?![:,\}])

显然,这段代码一点也不健壮(如果客户端发送
我必须在下午2点调用
,正则表达式将无法检测到未替换的报价),真正的解决方案是修复JSON本身,但希望这能帮助您在修复之前处理大部分数据。

您需要找出数据的来源并将其修复。它不是一个合适的JSON,因此不容易修复。为什么需要“清理”它“json?你得到的是无效数据吗?只要值本身中没有任何未替换的新行,json的格式就应该无关紧要。@aynber,我没有访问该程序的权限。然后你需要联系创建该程序的人,告诉他们他们创建的json无效。哎哟。如果他们能像那样传递数据,我打赌他们是在手动或类似的方式构建它。那样的话,你会玩得很开心。这将意味着在这一过程中可能会出现更多的问题。不要试图匹配他们在代码中的错误,而是告诉他们提供正确的数据。试图解决这样的问题可能非常困难(有时不可能实现自动化)。这个答案完全是关于
json\u encode()
。OP从外部源获取JSON数据(不受其控制),在解码而不是编码时遇到问题。
 $regex = '/(?<![ ,\\\\])"(?![:,\\}])/' ;
 $json = preg_replace($regex, '\"', $string);