B4J Question Json Parsing Error

Hi,
When i parse the json getting error : java.lang.NumberFormatException: For input string: "date"


B4X:
    Dim j As HttpJob
    j.Initialize("", Me) 'name is empty as it is no longer needed
    j.Download("https://www.timeapi.io/api/Time/current/zone?timeZone=Asia/Kolkata")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then

        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim jroot As List = parser.NextObject
        Dim date As String = jroot.Get("date")

    End If
    j.Release

When i parse the json in jsontool it parses without error and generates the json parsing code properly which is used above
 

DonManfred

Expert
Licensed User
Longtime User
generates the json parsing code properly which is used above
No. Your json does not include a List. Only a single map.

B4X:
dim json as string = $"{"year":2022,"month":5,"day":22,"hour":11,"minute":6,"seconds":7,"milliSeconds":589,"dateTime":"2022-05-22T11:06:07.589625","date":"05/22/2022","time":"11:06","timeZone":"Asia/Kolkata","dayOfWeek":"Sunday","dstActive":false}"$
Dim parser As JSONParser
parser.Initialize(json)
Dim jRoot As Map = parser.NextObject
Dim dateTime As String = jRoot.Get("dateTime")
Dim date As String = jRoot.Get("date")
Dim dstActive As String = jRoot.Get("dstActive")
Dim year As Int = jRoot.Get("year")
Dim timeZone As String = jRoot.Get("timeZone")
Dim minute As Int = jRoot.Get("minute")
Dim seconds As Int = jRoot.Get("seconds")
Dim dayOfWeek As String = jRoot.Get("dayOfWeek")
Dim month As Int = jRoot.Get("month")
Dim hour As Int = jRoot.Get("hour")
Dim time As String = jRoot.Get("time")
Dim day As Int = jRoot.Get("day")
Dim milliSeconds As Int = jRoot.Get("milliSeconds")
 
Upvote 1

Omar Parra A.

Expert
Licensed User
Longtime User
or new (AS)
B4X:
    j.Initialize("", Me) 'name is empty as it is no longer needed
    j.Download("https://www.timeapi.io/api/Time/current/zone?timeZone=Asia/Kolkata")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then

        Dim jroot As Map = j.GetString.As(JSON).ToMap 'new !!
    
        Dim DateTime1 As String = jroot.Get("dateTime")
        Dim date As String = jroot.Get("date")
        Dim dstActive As String = jroot.Get("dstActive")
        Dim year As Int = jroot.Get("year")
        Dim timeZone As String = jroot.Get("timeZone")
        Dim minute As Int = jroot.Get("minute")
        Dim seconds As Int = jroot.Get("seconds")
        Dim dayOfWeek As String = jroot.Get("dayOfWeek")
        Dim month As Int = jroot.Get("month")
        Dim hour As Int = jroot.Get("hour")
        Dim time As String = jroot.Get("time")
        Dim day As Int = jroot.Get("day")
        Dim milliSeconds As Int = jroot.Get("milliSeconds")

        Log(DateTime1)
        Log(date)
    
    End If
    j.Release
 
Upvote 1
Thanks Erel, DonManfred and Omar Parra A For the guidance.

It is RIGHT that, instead of MAP i was using LIST to parse the JSON.

Now in debug mode, it is working properly, Thank you
 
Upvote 0
Top