0

I have json string live below, I try to convert array but I am not success can anyone please help me, thank you advance.

Example 1 : s:59:"[{"item_id":"UTILITY CON CERNIERA","qty":1,"points":"110"}]";

Example 1 : s:109:"[{"item_id":"UTILITY CON CERNIERA","qty":1,"points":"110"},{"item_id":"PESA VALIGIA","qty":1,"points":"120"}]";
Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
Raju Dudhrejiya
  • 1,547
  • 2
  • 14
  • 16

5 Answers5

3

It seems, that you have a serialized json, so try this:

$array = json_decode(unserialize($string), true);

But it also seems that your data is corrupted, that's why unserialize does not work correctly in some PHP versions. If this is your case then in this question you can find a way to fix this: unserialize() [function.unserialize]: Error at offset.

Community
  • 1
  • 1
Gino Pane
  • 4,429
  • 4
  • 27
  • 42
2

Use unserialize and json_decode

json_decode(unserialize($string),true); // pass second argument true

When true, returned objects will be converted into associative arrays.

Ravi
  • 6,311
  • 1
  • 23
  • 41
0

Your data is serialized as well as json encoded. So you have to use:-

json_decodealong with unserialize like below:-

<?php

$data = 's:59:"[{"item_id":"UTILITY CON CERNIERA","qty":1,"points":"110"}]"';
print_r(json_decode(unserialize($data),true));
?>

And

<?php

$data = 's:109:"[{"item_id":"UTILITY CON CERNIERA","qty":1,"points":"110"},{"item_id":"PESA VALIGIA","qty":1,"points":"120"}]"';
print_r(json_decode(unserialize($data),true));
?>

https://eval.in/590757 And https://eval.in/590758

For more reference:-

http://php.net/manual/en/function.json-decode.php

http://php.net/manual/en/function.unserialize.php

Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
-1

seems like these are serialized string not json encoded..

use json_decode(unserialize($string)); to get array.

Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
Atul Cws
  • 7
  • 3
-1

Read http://php.net/manual/en/function.json-decode.php

$array=json_decode($JSON_STRING,true);

Second parameter in function is for getting result as an Array, by default it returns Object.

Alok Patel
  • 7,552
  • 5
  • 27
  • 47