2

i POST a JSON-String from an Android Application to a Webserver. There i want to decode the String into JSON and save the data into a database. But here is the Problem.

I read out the POST-Variable:

$json = $_POST['json'];

This brings me:

{"user":"Bob"}

Then i decode the json:

$decoded = json_decode($json, true);

There i get NULL!

BUT, when i create a String in PHP like:

$json = '{"user":"Bob"}';

and decode, it works?! But why? Where is the difference? Hope everyone can help me! :-/

Thanks a lot!

Thomas

Tschakle
  • 225
  • 1
  • 4
  • 14
  • http://stackoverflow.com/questions/983761/android-java-simple-send-and-recieve-with-server-fast-setup-challenge http://code.google.com/p/google-gson/ http://stackoverflow.com/questions/2818697/sending-and-parsing-json-in-android – Samir Mangroliya Sep 15 '11 at 08:28
  • What do you want to tell me with these links? – Tschakle Sep 15 '11 at 08:49
  • this link also send json string/./// – Samir Mangroliya Sep 15 '11 at 08:51
  • The 2 examples look identical to me, if `$_POST['json']` really is what you think it is. Is it possible there is a difference in character encoding?! – MrWhite Sep 15 '11 at 09:13
  • i save the string of $_POST['json'] in the database to see it, because i don't know an other way. so it can be that any character is wrong and i don't see it :-/ ... the function json_last_error() says me, that there is a JSON_ERROR_SYNTAX, so i thing i don't see everything :-/ ... how can i correctly see the string of $_POST['json']? – Tschakle Sep 15 '11 at 09:25
  • i solve the problem! --> $decoded = json_decode(stripslashes($json), true); – Tschakle Sep 15 '11 at 09:44
  • Ah, so there was a difference! :) Whether slashes are added in the beginning will be dependent on your web server, so you may need to check first. (You might as well put this as your answer and accept it.) – MrWhite Sep 15 '11 at 09:51

1 Answers1

0

You may be sending json data as a string just send it as it is don't use (') or (") inverted comas while passing the json data.

nsgulliver
  • 12,655
  • 23
  • 43
  • 64
keshu_vats
  • 452
  • 6
  • 45