B4J Question JSON Parse trims quotes

hatzisn

Expert
Licensed User
Longtime User
Take under notice the following JSON string:

JSON:
{"act":"1","data":{"field1":"1","field2":"2"}}

If you do this:

B4X:
Dim m As Map = sJSON.As(JSON).ToMap
log(m.Get("data"))

What you get in the logs is the {field1=1,field2=2}.
I tried to access it as a map but I cannot retrieve the values.
 

cklester

Well-Known Member
Licensed User
Take under notice the following JSON string:

JSON:
{"act":"1","data":{"field1":"1","field2":"2"}}

If you do this:

B4X:
Dim m As Map = sJSON.As(JSON).ToMap
log(m.Get("data"))

What you get in the logs is the {field1=1,field2=2}.
I tried to access it as a map but I cannot retrieve the values.

Can you make another map variable?

B4X:
Dim m As Map = sJSON.As(JSON).ToMap
Dim data as Map = m.Get("data")
Log(data.Get("field1"))
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Take under notice the following JSON string:

JSON:
{"act":"1","data":{"field1":"1","field2":"2"}}

If you do this:

B4X:
Dim m As Map = sJSON.As(JSON).ToMap
log(m.Get("data"))

What you get in the logs is the {field1=1,field2=2}.
I tried to access it as a map but I cannot retrieve the values.

I successfully retrieved the values. When you add at debug time a line it does not return the values. If you stop the code execution and restart the code it works.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
I successfully retrieved the values. When you add at debug time a line it does not return the values. If you stop the code execution and restart the code it works.

Apparently this was not the case. I tried it again and if you add a new line at debug time it seems to be working this time.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
direct access

B4X:
    Dim  sJSON As String = $"{"act":"1","data":{"field1":"1","field2":"2"}}"$
    
    Log(sJSON.As(JSON).ToMap.Get("act"))
    Log(sJSON.As(JSON).ToMap.Get("data").As(Map).Get("field1"))
    Log(sJSON.As(JSON).ToMap.Get("data").As(Map).Get("field2"))
1668103940211.png
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
?
direct access

B4X:
    Dim  sJSON As String = $"{"act":"1","data":{"field1":"1","field2":"2"}}"$
  
    Log(sJSON.As(JSON).ToMap.Get("act"))
    Log(sJSON.As(JSON).ToMap.Get("data").As(Map).Get("field1"))
    Log(sJSON.As(JSON).ToMap.Get("data").As(Map).Get("field2"))
View attachment 135870

Sorry for the LOL reaction to your post. It slipped from my mouse. Some times bugging or debugging can be hillarious. :)
I have already managed to get the values. What I was trying to do is reproduce the problem why I could not get the values previously. It seems that B4J has some attitude problems.
 
Upvote 0
Top