Android Question JSON - how to evaluate an empty map?

Stolli0815

Member
Licensed User
Longtime User
Hello community.


I give up - I can't figure out a way to determine, wether a JSON result is empty or not.

Sometimes, the server result is like that (a):
{"fragenIds":["346","359","315"],"assetIds":[]}
Sometimes, assetIds has some content (b):
{"fragenIds":["134","276","383","967"],"assetIds":{"51":{"name":"20"},"52":{"name":"8"},"72":{"name":"18"}}}

I can evaluate the content properly. But, if the server returns no content for assetIds as a result of a query, I'll have exceptions...

First try works well for content (b).
B4X:
Dim Parser As JSONParser
Dim mroot As Map
Dim mAssets As Map
Dim mHelp As Map

mRoot = Parser.NextObject
mAssets = mRoot.Get("assetIds")
Log("Size:" & mAssets.Size)               '...this line will be reported within exception

If (mAssets.Size > 0) Then
     For i = 0 To mAssets.Size - 1
        mHelp = mAssets.GetValueAt(i)
        Log("Asset ID: "& mHelp.GetValueAt(0) & " for name: " & mAssets.GetKeyAt(i))
    Next
End If
But with this implementation there will be an exception with (a): java.lang.ClassCastException: java.util.ArrayList cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap

Okay. If I now add a Dim lAssets as list and assign the same Get then to mAssets, and I check the Size of lAssets, then (a) works fine, but (b) throws an exception the other way around: java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map$MyMap cannot be cast to java.util.List

Has someone an Idea, how to distinguish those two cases? :-$


By the way: This is only a small part of a huge code module. Try & catch are used and will result in a user message and will lead to a recovery of a database. So, this is unfortunatelly not the solution...
 

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
Why does the server return this:

{"fragenIds":["346","359","315"],"assetIds":[]}

instead of

{"fragenIds":["346","359","315"],"assetIds":{}}

when "asetids" is empty?
 
Upvote 0
Top