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:



Thanks in advance
 
Solution
B4X:
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"))

Daestrum

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

Magma

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

Daestrum

Expert
Licensed User
Longtime User
inside a string \" is treated as the character " and not the string terminator.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Erel's special tool has different prospect...

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



so removing double quotes... getting this




Do you have any other example for json parsing.. ?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
you could (probably) just .replace($"\""$,chr(34)) in the string from httpjob
 
Upvote 0

Magma

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

Daestrum

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

Magma

Expert
Licensed User
Longtime User
Ok... tried to make it simple and as fast i can...

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

Attachments

  • test.zip
    221 bytes · Views: 32
Upvote 0

aeric

Expert
Licensed User
Longtime User
When you make Post request, try adding
B4X:
job.GetRequest.SetContentType("application/json")

Code Snippets from OkHttpUtils2:
        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

Magma

Expert
Licensed User
Longtime User
Hi there, not trying to Post request... but Download - not different like fetching (like Erel's jsontree)
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
This is what I get from your original line in file

(MyMap) {Pass=xxxxxxxxxxxxxxxxx, Username=xxxxxxxxxxxxxx, IatrosId=1, Id=33, Admin=false}
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…