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:
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
B4X:
    Dim orig As String = $"[{\"Id\":33,\"Username\":\"xxxxxxxxxxxxxx\",\"Pass\":\"xxxxxxxxxxxxxxxxx\",\"Admin\":false,\"IatrosId\":1}]"$
    Dim js As String = orig.SubString2(1,orig.Length-1)
    Log(js)
    js = js.Replace($"\""$,$"""$)
    Log(js)
    Dim parser As JSONParser
    parser.Initialize(js)
    Dim m As Map = parser.NextObject
    Log(m.Get("IatrosId"))
    Log(m)
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
B4X:
    Dim orig As String = $"[{\"Id\":33,\"Username\":\"xxxxxxxxxxxxxx\",\"Pass\":\"xxxxxxxxxxxxxxxxx\",\"Admin\":false,\"IatrosId\":1}]"$
    Dim js As String = orig.SubString2(1,orig.Length-1)
    Log(js)
    js = js.Replace($"\""$,$"""$)
    Log(js)
    Dim parser As JSONParser
    parser.Initialize(js)
    Dim m As Map = parser.NextObject
    Log(m.Get("IatrosId"))
    Log(m)
It that the right solution or the only solution ?

because removing/replacing the \" (those characters with ") ...not seems right... or I am saying it wrong ?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
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"))
 
Upvote 0
Solution

Magma

Expert
Licensed User
Longtime User
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"))
WOW! you did it !
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
My two cents.
I've encountered several issues of this type.

It is always recommended to parse the JSON string as an object before converting it to map or list type.

B4X:
    Dim thestring As String = $""[{\"Id\":33,\"Username\":\"xxxxxxxxxxxxxx\",\"Pass\":\"xxxxxxxxxxxxxxxxx\",\"Admin\":false,\"IatrosId\":1}]""$
    Dim Parser As JSONParser
    Parser.Initialize(thestring)
    Log(Parser.NextValue.As(JSON).ToList)

test
B4X:
    Log(Parser.NextValue.As(JSON).ToList.As(JSON).ToString)

1728501556198.png


ref.
 
Last edited:
Upvote 0
Top