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"}]";
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Raju Dudhrejiya
  • 1,637
  • 2
  • 15
  • 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,740
  • 5
  • 31
  • 46
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 Hirani
  • 6,511
  • 1
  • 27
  • 42
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

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
-1

seems like these are serialized string not json encoded..

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

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
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,842
  • 5
  • 31
  • 47