Android Question Json UTF8 Download with httpjob has many "\" into string

Magma

Expert
Licensed User
Longtime User
Hi there,

probably I am doing something wrong as always...

Json UTF8 Download with httpjob has many "\" into string, but browser can read it without them. (as JSON).. also in start and end have [, ]

1. Is the right way removing [ ] (array/list) with substring2 or i must use parser --- can i have an example in forum for one record (object) and for more ?
2. Those "\" can removed automatically and how ?

What i get with httpjob:
"[{\"Id\":33,\"Username\":\"xxxxxxxxxxx\",\"Pass\":\"xxxxxxxxx\",\"Admin\":false,\"IatrosId\":1}]"

What i get with browser:

1728464451103.png


Thanks in advance
 
Solution
B4X: Copy
Dim thestring As String = $""[{\"Id\":33,\"Username\":\"xxxxxxxxxxxxxx\",\"Pass\":\"xxxxxxxxxxxxxxxxx\",\"Admin\":false,\"IatrosId\":1}]""$
Log(thestring)
Dim parser As JSONParser
parser.Initialize(thestring)
Dim value As String = parser.NextValue
parser.Initialize(value)
Dim array1 As List = parser.NextArray
Dim map1 As Map = array1.Get(0)
Log(map1.Get("Username"))
If you notice the browser string - it is enclosed in single quotes, and therefore does not need to escape the double quotes. The string returned from httpjob uses " (double quote) so has to escape the " within the string
 
Upvote 0
If you notice the browser string - it is enclosed in single quotes, and therefore does not need to escape the double quotes. The string returned from httpjob uses " (double quote) so has to escape the " within the string
that means ? :)
 
Upvote 0
inside a string \" is treated as the character " and not the string terminator.
 
Upvote 0
Erel's special tool has different prospect...

First if i leave double quotes (download of httpjob has double) in start and at the end

1728467511737.png


so removing double quotes... getting this

1728467354012.png



Do you have any other example for json parsing.. ?
 
Upvote 0
you could (probably) just .replace($"\""$,chr(34)) in the string from httpjob
 
Upvote 0
What if you Try the B4X Library and post the result from this?

Best is to upload a small project showing the issue
I will do my best... the problem is that the server is not mine and is for development use...

So I will pass the download and go to parsing the "string downloaded" will copy / paste

wait for my results... coming soon
 
Upvote 0
Hmmm.. that means if someone has a value in string perhaps at... Pass "%$%#$\123" ..will change his pass ! (?)
no unless their password contained \" ie abcd\"efg
 
Upvote 0
Ok... tried to make it simple and as fast i can...

Code: Copy
    Dim thestring As String=File.ReadString(File.DirApp,"test.json") ' i attach the test.json into zip
    Log(thestring)
    
    Dim parser As JSONParser
    parser.Initialize(thestring.SubString2(2,thestring.Length-2))  'removing the [, ]  - if not remove getting the same error as first dialog or with removing taking the second...
    Dim m As Map=parser.NextObject
    Log(m.Get("Username"))


what get in log:
WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
Waiting for debugger to connect...
Program started.
"[{\"Id\":33,\"Username\":\"xxxxxxxxxxxxxx\",\"Pass\":\"xxxxxxxxxxxxxxxxx\",\"Admin\":false,\"IatrosId\":1}]"
Error occurred on line: 40 (Main)
org.json.JSONException: Expected literal value at character 1 of {\"Id\":33,\"Username\":\"xxxxxxxxxxxxxx\",\"Pass\":\"xxxxxxxxxxxxxxxxx\",\"Admin\":false,\"IatrosId\":1}
at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
at org.json.JSONTokener.readLiteral(JSONTokener.java:285)
at org.json.JSONTokener.nextValue(JSONTokener.java:111)
at org.json.JSONTokener.readObject(JSONTokener.java:362)
at org.json.JSONTokener.nextValue(JSONTokener.java:100)
at anywheresoftware.b4j.objects.collections.JSONParser.NextObject(JSONParser.java:65)
at b4j.example.main._appstart(main.java:123)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
at b4j.example.main.start(main.java:38)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:1589)
 

Attachments

Upvote 0
When you make Post request, try adding
B4X: Copy
job.GetRequest.SetContentType("application/json")

Code: Copy
        Dim job As HttpJob
        job.Initialize("", Me)
        Dim payload As Map = CreateMap("key": "value", "another key": 1000)
        Dim json As String = payload.As(JSON).ToString 'make sure that the json library is checked
        job.PostString("https://link here", json)
        job.GetRequest.SetContentType("application/json")
        Wait For (job) JobDone (job As HttpJob)
        If job.Success Then
            'assuming that the response is json:
            Dim response As Map = job.GetString.As(JSON).ToMap
            Log(response)

        End If
        job.Release
 
Upvote 0
When you make Post request, try adding
B4X: Copy
job.GetRequest.SetContentType("application/json")

Code: Copy
        Dim job As HttpJob
        job.Initialize("", Me)
        Dim payload As Map = CreateMap("key": "value", "another key": 1000)
        Dim json As String = payload.As(JSON).ToString 'make sure that the json library is checked
        job.PostString("https://link here", json)
        job.GetRequest.SetContentType("application/json")
        Wait For (job) JobDone (job As HttpJob)
        If job.Success Then
            'assuming that the response is json:
            Dim response As Map = job.GetString.As(JSON).ToMap
            Log(response)

        End If
        job.Release
Hi there, not trying to Post request... but Download - not different like fetching (like Erel's jsontree)
 
Upvote 0
This is what I get from your original line in file

(MyMap) {Pass=xxxxxxxxxxxxxxxxx, Username=xxxxxxxxxxxxxx, IatrosId=1, Id=33, Admin=false}
 
Upvote 0
Back
Top