- Home /
Unity Php post JSON string problem (incomplete string)
Hi, I haven't been able to find a solution for this particular problem, so here goes:
My problem is that when I use unity to post data to a database through a PHP page, the complete string isn't uploaded. This is best illustrated with an example:
WWW playerUpdateRequest = new WWW("http://******/testJSON.php?test=" + jsonString);
Where my jsonString item, is formatted correctly, and starts something like this:
{"Graveyards":[{"SizeID":1, "PlotCount":9, "GraveyardID":1, "Plots":[{.....
Now, I tried storing the posted string in a database, and the result is that I only get the first part of the string:
{"Graveyards":[{"SizeID":1,
That's the entire content of the table item!
If I do it manually by going to that page and running
testJSON.php?test={"Graveyards":[{"SizeID":1, "PlotCount":9, "GraveyardID":1, "Plots":[{.....
Then it works fine and my database record shows the complete string (with any commas and quotes preserved).
This means that the php argument works fine, and that the database string insertion works fine as well (since I can get a complete hole through by doing it manually). For completeness sake in case quotes caused problems, I did try a string replacement, and replaced all quotes with ^, but this changes nothing. The database still holds only a fraction of the json string.
This leaves me isolating the problem to how unity posts the WWW request. Therefore I have put this post here, and I hope someone can help me solve this mystery :)
Answer by flamy · Nov 06, 2013 at 07:06 AM
WWW playerUpdateRequest = new WWW("http://******/testJSON.php?test=" + jsonString);
change this to
WWW playerUpdateRequest = new WWW("http://******/testJSON.php?test=" + WWW.EscapeURL(jsonString));
check more details on this page. The problem seems to be with the spaces in the string.
If this doesnt solve the issue, try printing the value of jsonString and check if it is complete.
Goddamn flamy you just saved my day! Thank you :)
It works now, Now I get a "414 request-uri too long", but that's a webserver problem and I can handle that myself.
Thanks a lot :)
Hi how will you declare json string like "hi#bhyiuyiu&kljkmk J$$anonymous$$LJL$$anonymous$$Oo;ikmp;io &^7676nlkjl;iuoinui36#@&(&&%&%*&%&*n hjkghiugiu"
@ben.aten: I'm sorry, but, what?? The "string" you posted there is not json and it looks like it hasn't any meaning since you just typed a random string. If you have a question, feel free to ask it as seperate question and don't post a comment to an answer on a completely different problem.
If you want to ask a question be more specific than this. Json is just a way to structure different data in a text format. It only supports some basic types (number, string, bool, array, object). A string value has to be escaped so it doesn't contain any of the special characters that make up the structure of the json file itself. It's explained in the RFC7159 section 7.
$$anonymous$$y SimpleJSON framework has these escape rules implemented.