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"}]";
4
  • What you tried so for? put that code here. Commented Jun 17, 2016 at 10:33
  • 1
    That's not JSON. That looks like a serialize()'d JSON string. Commented Jun 17, 2016 at 10:34
  • What's outputting this? There's JSON, but it's inside a serialize()'d string, prepended with something else... Commented Jun 17, 2016 at 10:34
  • Thank you it's working using unserialize() Commented Jun 17, 2016 at 10:44

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

This one returns an array as requested.
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

Comments

-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.

Comments

-1

seems like these are serialized string not json encoded..

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

2 Comments

unserialize will get you a string here, not an array.
yes @Gino Pane is correct it has serialized as well as json encoded string but you need to unserialize first

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.