B4J Question Escaping characters in JSON

j_o_h_n

Active Member
Licensed User
Longtime User
I have two B4J applications
One puts a questionnaire into JSON and that gets posted to the other one which is a webserver.
The other one then extracts the contents from the JSON and displays the questionnaire to visitors.
The questions in the questionnaires are written by users not by me.
The problem I came across yesterday was when someone put an ampersand into the text of a question

Everything seems to work fine on the client side

B4X: Copy
Log(Text)  ' looks OK, text includes ,"questiontext":"What is 2 & 2"}

job2.Initialize("Job2", Me)

job2.PostString(FullUrl & "/questionnaireupload",Text)

But on the server side

B4X: Copy
Dim squestionnaireupload As String = req.GetParameter("questionnaire")

log(squestionnaireupload )  'terminates in ,"questiontext":"What is 2


And as soon as you try to parse it you get the following error
org.json.JSONException: Unterminated string at character 284 of {"questionnairename etc

Do I need to escape characters on the client side before sending or is there something else I need to do?
If escaping of characters is required, is there a list somewhere of the ones that need to be escaped.
I saw one such list online but it did not include the ampersand in it.
Thanks for any help.
 
Thanks Erel.
On the client side Log(text) gives :
bizid=62&password=$2a$10$AbturmopxJgYdSdKB&clientver=1&questionnaire={"questionnairename":"Silly questionnaire","duration":"36","bizid":"62","phoneid":"6666","questions":[{"displayno":false,"displaydontknow":false,"questioninfo":"","displaycomment":true,"answer":"","displayyes":false,"questionnum":"1","comment":"","questiontext":"What is 2 & 2"},{"displayno":true,"displaydontknow":true,"questioninfo":"Think carefully now!","displaycomment":false,"answer":"","displayyes":true,"questionnum":"2","comment":"","questiontext":"is 5 less than 3"}],"failedloginslimit":"3","urlid":"","questionnaireinfo":"try it out!"}

On the server side Log(req.GetParameter("questionnaire")) gives:
{"questionnairename":"Silly questionnaire","duration":"36","bizid":"62","phoneid":"6666","questions":[{"displayno":false,"displaydontknow":false,"questioninfo":"","displaycomment":true,"answer":"","displayyes":false,"questionnum":"1","comment":"","questiontext":"What is 2

There's a space after the 2 at the end.
 
Upvote 0
If you control the client and the server then move all parameters to the json string and set the content type to application/json.

You should then read the payload with:
Code: Copy
 Dim jp As JSONParser
   Dim data() As Byte = Bit.InputStreamToBytes(req.InputStream)
   jp.Initialize(BytesToString(data, 0, data.Length, "UTF8"))

Otherwise you need to properly build an application/x-www-form-urlencoded encoding payload and escape the ampersands.
 
Upvote 0
Thanks very much, Erel, I'll do as you suggest.

Later Edit: This is to confirm that what Erel said to do, fixed the problem.:)
 
Last edited:
Upvote 0
Back
Top