简体   繁体   English

解析 json 由于 unicode 个字符和斜杠而损坏的字符串

[英]Parse json string which is broken due to unicode characters and slashes

I'm trying to received data from Android to Laravel using API. Below are the data that I received in format object array list.我正在尝试使用 API 接收从 Android 到 Laravel 的数据。以下是我以 object 数组列表格式接收的数据。

$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; name\u003d\"certificateFile\"; filename\u003d\"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": {} }';

I already attempted to decode the object, but it returned NULL .我已经尝试解码 object,但它返回NULL

Here's what I tried:这是我尝试过的:

var_dump(json_decode($obj, true));

Bruteforce trial and error json repair at its finest: Bruteforce 反复试验 json 修复最佳状态:

The problem stems from those pesky unicode = ( = ) characters.问题源于那些讨厌的 unicode = ( = ) 字符。

I needed to replace them and handle slashes.我需要更换它们并处理斜杠。


...In case I need to clarify for anyone... ...以防万一我需要为任何人澄清...

THIS IS A HACK!!!!这是一个黑客!!!!

Code: ( Demo )代码:(演示

$fixed = preg_replace('/\\\\u003d\\\\\\\\(.*?)\\\\\\\\/', '=\\\\\\\\\\\\$1\\\\\\\\\\\\', $obj);

var_export(
    json_decode(json_decode($fixed)->registerDetails)
);

Output: Output:

(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,
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM