Tell the solution how to parse JSON in PHP, which comes across this kind of thing:

{"accDo": "a:2:{s:3:"get";s:1:"1";s:4:"take";s:1:"1";}"} 

Standard tools return a syntax error.

  • 2
    [json_encode ()] [1]? [1]: php.net/manual/ru/function.json-encode.php - Deonis
  • just the problem with json_decode. - nddd
  • @nddd who generates json? - etki
  • I have no idea, I take it as a string from the base. - nddd
  • one
    @nddd, Check - do you have any processing with stripcslashes ()? - Deonis

1 answer 1

@nddd , I guess what the problem is. It was about the following:

 $arr = array ( 'accDo' => array ( 'get' => '1', 'take' => '1', ) ); foreach($arr as $k => $v){ $arr[$k] = serialize($v); } 

Result:

 {"accDo":"a:2:{s:3:\"get\";s:1:\"1\";s:4:\"take\";s:1:\"1\";}"} 

Slashes were deleted either before being added to the database, or after the output. Who ever thought of converting with two methods? ..

Update

@nddd , there are more competent people on the regular forum, but for now I can offer this option:

 // Исходная строка $wrongStr = '{"accDo":"a:2:{s:3:"get";s:1:"1";s:4:"take";s:1:"1";}"}'; // К сериализованной части применяем addslashes() $normalStr = preg_replace_callback( '/a:\d+:([^}]+)}/', create_function( '$matches', 'return addslashes($matches[0]);' ), $wrongStr ); // декодирование $arr = json_decode($normalStr, 1); print_r( unserialize($arr['accDo']) ); 

Result

 Array ( [get] => 1 [take] => 1 ) 

But I repeat that it would be good to finalize the regular season.

  • Thank you, this solution is what you need. ) - nddd