B4J Question Error creating json and "\"

victormedranop

Well-Known Member
Licensed User
Longtime User
Hi i'm using this function to create a json string

Code: Copy
    Dim m As Map =         CreateMap("caption":caption,"type":"image/png","recipients":recipients,"content":content)
    Dim generator As JSONGenerator
    generator.Initialize(m)
[code]

but when i print the json

apears like this 

{
 "recipients": "18099897215",
 "caption": "Popola 1",
 "type": "image\/png",
 "content": "iVBORw0KGg'
}

see the type add "\" to the json string there for the web service respond with an error.

any help will be apreciated.

Victor
 
i try with replace functions but did not works and i
Dont think thats an option for me. beacuse i an sending base64 images.

Victor

still searching .......
 
Upvote 0
i try with replace functions but did not works and i
Code example? Output of base64 string before adding it to JSONGenerator? Output after JSONGenerator? Output after replacement function on the output of JSONGenerator?
 
Upvote 0
check the function

Code: Copy
Sub post_image(recipients As String , caption As String, content As String )
    Dim j As HttpJob
    j.Initialize("", Me)
    Dim m As Map = CreateMap("caption":caption,"type":"image/png","recipients":recipients,"content":content)
    Dim generator As JSONGenerator
    generator.Initialize(m)
   
    Dim ss As String = generator.ToString
    ss.Replace("\","")
    Log("String to send "&ss)
    j.PostString("http://64.6.221.124:1744/img/listPOST",ss)
    j.GetRequest.SetContentType("application/json")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Try
            Dim parser As JSONParser
            Log("Serial Result "&j.GetString)
            parser.Initialize(j.GetString)
            Dim root As Map = parser.NextObject
            Dim msg_id As String = root.Get("msg_id")
            Log($"update messages set id_message =  '${msg_id}' ,estatus = 1, fecha_recepcion = 'now()'  where telefono = '${telefono}' and mensaje = '${mensaje}'"$)
            sql1.ExecNonQuery($"update messages set id_messages =  '${msg_id}' ,status = 1, fecha_recepcion = now()  where telefono = '${telefono}' and mensaje = '${mensaje}'"$)
        Catch
            Log(LastException)
        End Try
    End If
    j.Release
    Sleep(1000)
End Sub
 
Upvote 0
It’s supposed to be
Code: Copy
ss.Replace(“\/“,”/“)
You may also need
Code: Copy
ss.Replace(“\\”,”\”)
Note: typing this from my phone, so don’t copy paste this since quotation marks may be wrong.

Those were the two replacement cases mentioned in the linked article.
 
Upvote 0
Replace wont change the content of the string

Either use
Code: Copy
ss = ss.replace("\","")
or
Code: Copy
 Dim ss As String = generator.toString.Replace("\","")

or to save creating a new string variable change this line
Code: Copy
    j.PostString("http://64.6.221.124:1744/img/listPOST",ss)
to
Code: Copy
    j.PostString("http://64.6.221.124:1744/img/listPOST",generator.toString.Replace("\",""))
 
Last edited:
Upvote 0
@Daestrum: good catch, I missed that one. @victormedranop: The replacement pattern(s) still needs to be as I posted.
 
Upvote 0
i couldnt go to bed.....naaa the same error
Code: Copy
Log("String to send "&ss)
    j.PostString("http://64.5.122.233:3044/img/listPOST",ss.Replace("\",""))
    j.GetRequest.SetContentType("application/json")
String to send {"recipients":"18099897215","caption":"Pop1","type":"image\/png","content":"iVBORw0KuVcR7AXug1eB7lGnJNqDFNBuq8CcV6DdGsDN02GnAGlZgrmsQl2WgC6LYOcVmEvg...
 
Upvote 0
sql1.ExecNonQuery($"update messages set id_messages = '${msg_id}' ,status = 1, fecha_recepcion = now() where telefono = '${telefono}' and mensaje = '${mensaje}'"$)
You should use parameterized queries instead.

This should work:
Code: Copy
StringToSend = StringToSend.Replace("\","")
Log(StringToSend)
 
Upvote 0
Works..... but need to explain.

whats the diference with

Code: Copy
j.PostString("http://64.6.221.124:1744/img/listPOST",generator.toString.Replace("\",""))

and

ss = ss.Replace("\","")

thanks erel.

Victor
 
Upvote 0
ss.Replace("\","")
Even though this works in this single case, please note that this can cause issues with other cases where you may want to clean up JSONGenerator's escaping
Code: Copy
    Dim m As Map = CreateMap("sometext":"Java's JSON handling escapes / to \/ and \ to \\")
   Dim generator As JSONGenerator
   generator.Initialize(m)
   Log($"Original Text: ${m.Get("sometext")}"$)
   Log($"JSONGenerator.ToString: ${generator.ToString}"$)
   Log($"Replace("\",""): ${generator.ToString.Replace("\","")}"$)
   Log($"Replace("\/", "/").Replace("\\","\"): ${generator.ToString.Replace("\/", "/").Replace("\\","\")}"$)
Output:
Original Text: Java's JSON handling escapes / to \/ and \ to \\
JSONGenerator.ToString: {"sometext":"Java's JSON handling escapes \/ to \\\/ and \\ to \\\\"}
Replace("\",""): {"sometext":"Java's JSON handling escapes / to / and to "}
Replace("\/", "/").Replace("\\","\"): {"sometext":"Java's JSON handling escapes / to \/ and \ to \\"}
 
Upvote 0
Back
Top