Android Question JSONParser Error

SMOOTSARA

Active Member
Licensed User
Longtime User
Hello
I have the following json

B4X:
{"icon_link":"https:\/\/xxxx.com\/xxxx_applications\/xxxx\/xxxxx\/weather 128x128\/color\/","icon_format":".png"}


Everything is fine in the code below


B4X:
                Dim parser As JSONParser
                parser.Initialize(job1.GetString)
                Dim root As Map = parser.NextObject
                Dim icon_link As String = root.Get("icon_link")
                Dim icon_format As String = root.Get("icon_format")

But I get an error in the second code


B4X:
                Dim parser As JSONParser
                parser.Initialize(job1.GetString)
                Dim root As Map = parser.NextObject
                Dim icon_link As String = root.Get("icon_link")
                Dim icon_format As String = root.Get("icon_format")
                
                Dim cdc As Map = parser.NextObject

B4X:
Error occurred on line: 902 (Starter)
org.json.JSONException: End of input at character 140 of {"icon_link":"https:\/\/xxx.com\/xxxxx\/xxx\/xxxxx\/weather 128x128\/color\/","icon_format":".png"}
    at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
    at org.json.JSONTokener.nextValue(JSONTokener.java:97)
    at anywheresoftware.b4a.objects.collections.JSONParser.NextObject(JSONParser.java:65)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6138)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
 

DonManfred

Expert
Licensed User
Longtime User
But I get an error in the second code
The error is in the last line. You are trying to get ANOTHER map from the json. But there is no 2nd map inside the json.

Use just
B4X:
Dim parser As JSONParser
                parser.Initialize(job1.GetString)
                Dim root As Map = parser.NextObject
                Dim icon_link As String = root.Get("icon_link")
                Dim icon_format As String = root.Get("icon_format")
root is the map you want to get as cdc

That said why not use this instead

B4X:
Dim parser As JSONParser
                parser.Initialize(job1.GetString)
                Dim cdc As Map = parser.NextObject
                Dim icon_link As String = cdc.Get("icon_link")
                Dim icon_format As String = cdc.Get("icon_format")
 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
other simple:
B4X:
    Dim s As String = $"{"icon_link":"https:\/\/xxxx.com\/xxxx_applications\/xxxx\/xxxxx\/weather 128x128\/color\/","icon_format":".png"}"$
 
    Log(s.As(JSON).ToMap.Get("icon_link"))
    Log(s.As(JSON).ToMap.Get("icon_format"))
1639909738821.png


or
B4X:
    Dim s As String = job1.GetString
    Log(s.As(JSON).ToMap.Get("icon_link"))
    Log(s.As(JSON).ToMap.Get("icon_format"))

or
B4X:
    Dim mRoot As Map = job1.GetString.As(JSON).ToMap
    Log(mRoot.Get("icon_link"))
    Log(mRoot.Get("icon_format"))
 
Last edited:
Upvote 0
Top