B4J Question Error creating json and "\"

victormedranop

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

B4X:
    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
 

victormedranop

Well-Known Member
Licensed User
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

OliverA

Expert
Licensed User
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

victormedranop

Well-Known Member
Licensed User
check the function

B4X:
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

OliverA

Expert
Licensed User
It’s supposed to be
B4X:
ss.Replace(“\/“,”/“)
You may also need
B4X:
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

Daestrum

Well-Known Member
Licensed User
Replace wont change the content of the string

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

or to save creating a new string variable change this line
B4X:
    j.PostString("http://64.6.221.124:1744/img/listPOST",ss)
to
B4X:
    j.PostString("http://64.6.221.124:1744/img/listPOST",generator.toString.Replace("\",""))
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
@Daestrum: good catch, I missed that one. @victormedranop: The replacement pattern(s) still needs to be as I posted.
 
Upvote 0

victormedranop

Well-Known Member
Licensed User
i couldnt go to bed.....naaa the same error
B4X:
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

Erel

Administrator
Staff member
Licensed User
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:
B4X:
StringToSend = StringToSend.Replace("\","")
Log(StringToSend)
 
Upvote 0

victormedranop

Well-Known Member
Licensed User
Works..... but need to explain.

whats the diference with

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

and

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

thanks erel.

Victor
 
Upvote 0

OliverA

Expert
Licensed User
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
B4X:
    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
Top