0

I have some very odd issue,

I'm trying to parse some json string with php, string is in array, and if I use simple

json_decode($my_array[0],true)

it doesn't work, but, if I just copy a string from var_dump($my_array) and try to decode it works 100% ok.

Any ideas what could be wrong ?

Json string:

 {"mode":"view","pid":"243","documentId":"193"}
10
  • What exactly it doesn't work? What are you expecting? Commented Nov 29, 2012 at 13:46
  • "doesn't work" is meaningless. Commented Nov 29, 2012 at 13:46
  • My guess is that your JSON string has some unexpected invisible (white space) characters that are causing it to be invalid JSON in the first case, but aren't being carried across when you copy+paste it, and thus the copied+pasted version works. Commented Nov 29, 2012 at 13:54
  • "doesn't work" means I don't get any return object/array/string/anything , if I make var_dump(json_decode($my_array[0],true)) I just get int(0). Commented Nov 29, 2012 at 13:55
  • 0 is something, not nothing. Commented Nov 29, 2012 at 13:55

2 Answers 2

2

Kuba,

Here's the syntax for converting a json array into a php associative array:

$my_array = '{"mode":"view","pid":"243","documentId":"193"}';
$new_array = json_decode($my_array,true);

echo $new_array['mode']; //return: view
echo $new_array['pid']; //return: 243
echo $new_array['documentId']; //return: 193

var_dump() result:

array (size=3)
  'mode' => string 'view' (length=4)
  'pid' => string '243' (length=3)
  'documentId' => string '193' (length=3)
Sign up to request clarification or add additional context in comments.

1 Comment

I know syntax and how to do it, I've used it couple of times ;-) It seems that It could be something about white spaces (as SDC said in comments)
0

Ok, I've found the solution, after data serialization I've encoded string using base64_encode and then push It in that form to other scripts.

I still don't know why I had to encode it with base64, maybe because this string is used in javascript scripts and after that decoded in php?

Comments

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.