Android Question JSONParser return array or simple text

aeric

Expert
Licensed User
Longtime User
When I am using HttpUtils2, the JSON return from JobDone can be array or a simple text.

B4X:
Sub JobDone(Job As HttpJob)
    Dim ret As String
    Dim jsp As JSONParser   
    If Job.Success = True Then
        ret = Job.GetString
        jsp.Initialize(ret)       
        Select Case Job.JobName
            Case "MyJob"
                'ret = jsp.NextValue
                Dim lst As List
                lst = jsp.NextArray
                For i = 0 To lst.Size - 1
                    Dim M As Map
                    M = lst.Get(i)
                    intID = M.Get("id")
                Next
            Case Else
                Log("Missing Job=" & Job.JobName)
                ToastMessageShow("Error: Invalid Job", True)
        End Select
    Else
        Log("Error: (" & JobName & ") " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

How can I check is it an array or simple text? Is there any function such as isArray ?
B4X:
If isArray(jsp) = True Then
    Dim lst As List 
    lst = jsp.NextArray
Else
    strReturn = jsp.NextValue
End If
 

DonManfred

Expert
Licensed User
Longtime User
Maybe you need to check what the jsonparse returns...

maybe somethink like this

B4X:
    If jsonornot.StartsWith("{") Then
        Log("Seems to be a json result")
        ' Seems to be json
        Dim jsp As JSONParser
        jsp.Initialize(jsonornot)
        Dim test As Object= jsp.NextObject
        If test Is Map Then
            Dim m As Map = test
            Log("Map:"&m)
        else if test Is List Then
            Dim l As List = test
            Log("List:"&l)
        else if test Is String Then
            Dim text As String  = test
            Log("String:"&text)
        Else
            Log(GetType(test))       
        End If
    Else
        Log("Seems to be not a json result. Checking NextValue")
        Dim jsp As JSONParser
        jsp.Initialize(jsonornot)
        Dim text As String = jsp.NextValue
        Log("String:"&text)
    End If
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Check thé array size, if its 1, its à single string
The array can have size = 1
B4X:
lst = jsp.NextArray
If lst.Size = 1 Then
    ' The result like this [{"id":"1","col1":"val1","col2":"val2"}] can appear here
Else
   ' Expect list with size > 1 or size = 0
End If
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Thanks guys. Based on DonManfred idea, i do something like this:
B4X:
ret = Job.GetString
' Assume simple text does not contain this character
If ret.Contains("[") = True Then
    isArray = True
End If
jsp.Initialize(ret)

B4X:
If isArray = True Then
    Dim lst As List
    lst = jsp.NextArray
Else
    Dim str As String
    str = jsp.NextValue
End If
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
You've got full control of what you answer to the request:

Single value:

B4X:
Print 'OK';

OK = just a simple string

Array:

B4X:
Print JSON_ENCODE(array('Result' => 'OK'));

{"Result":"OK"} = map (or array)

List:

B4X:
Print JSON_ENCODE(array(array('Result' => 'OK')));

[{"Result":"OK"}] = list with a map (or array in an array)

And you know what job you've sent, so you know what you get back, too. Even if there's an SQL-Error. Return it like this:

B4X:
 ... or die(JSON_ENCODE(array('Error' => mysqli_error())));

and you'll get an array
 
Upvote 0
Top