解析由于unicode字符和斜杠而中断的json字符串



我正在尝试使用API从Android接收数据到Laravel。下面是我在格式化对象数组列表中收到的数据。

$obj = '{ "registerDetails": "{"accountName":"hh h hc","accountNumber":"868686","addressUser":"cg h jc","bankId":1,"selectedCityId":1,"emergencyName":"g g h","emergencyNumber":"0686868","education":[{"certificatePicture":{"body":{},"headers":{"namesAndValues":["Content-Disposition","form-data; nameu003d"certificateFile"; filenameu003d"2022-11-30-00-56-28-598.jpeg""]}},"grade":"hxycucu","educationLvl":"STPM / A Level or Equivalent","endEducation":"Dec 2022","id":4,"institutionName":"yxyxyxy","isEducationExpandable":false,"startEducation":"Nov 2022"}],"skill":[{"id":8},{"id":10}],"work":[{"companyName":"cuvuv","jobEndDate":"Nov 2022","isCurrentlyWorkHere":true,"isJobExpand":true,"jobPosition":"g cuc","jobScope":"cuccu","jobTitle":"6ff7f","jobStartDate":"Oct 2022"}],"phoneUser":"906886","postCode":"058686","selectedStateId":1}", "myKadFile": {}, "selfieFile": {} }';

我已经尝试解码对象,但它返回NULL

这是我尝试的:

var_dump(json_decode($obj, true));

brute - force trial and error json repair at its finest:

问题源于那些讨厌的unicode=(u003d)字符。

我需要替换它们并处理斜杠。


<子>…如果我需要为任何人澄清…

这是一个HACK!!!!

代码(演示):

$fixed = preg_replace('/\\u003d\\\\(.*?)\\\\/', '=\\\\\\$1\\\\\\', $obj);
var_export(
json_decode(json_decode($fixed)->registerDetails)
);

输出:

(object) array(
'accountName' => 'gsyau',
'accountNumber' => '168454',
'addressUser' => 'test',
'bankId' => 1,
'selectedCityId' => 1,
'emergencyName' => 'test',
'emergencyNumber' => '0146542346',
'education' => 
array (
0 => 
(object) array(
'certificatePicture' => 
(object) array(
'body' => 
(object) array(
),
'headers' => 
(object) array(
'namesAndValues' => 
array (
0 => 'Content-Disposition',
1 => 'form-data; name="certificateFile"; filename="2022-11-29-21-18-35-294.jpg"',
),
),
),
'grade' => 'test',
'educationLvl' => 'Doctoral (PHD) or Equivalent',
'endEducation' => 'Oct 2022',
'id' => 8,
'institutionName' => 'test',
'isEducationExpandable' => false,
'startEducation' => 'Aug 2022',
),
),
'skill' => 
array (
0 => 
(object) array(
'id' => 7,
),
1 => 
(object) array(
'id' => 9,
),
),
'work' => 
array (
0 => 
(object) array(
'companyName' => 'test',
'jobEndDate' => 'Oct 2022',
'isCurrentlyWorkHere' => false,
'isJobExpand' => false,
'jobPosition' => 'testtest',
'jobScope' => 'test',
'jobTitle' => 'test',
'jobStartDate' => 'Aug 2022',
),
),
'phoneUser' => '014654264',
'postCode' => '68100',
'selectedStateId' => 1,
)

相关内容

  • 没有找到相关文章

最新更新