Android Question Help with JSON Data

Colin Evans

Active Member
Licensed User
Longtime User
Hi could someone please help me I want to get the value of Count in the following JSON data

B4X:
{"count":0,"next":null,"previous":null,"results":[]}

which parsers as

B4X:
[LIST]
[*]next: null
[*]previous: null
[*]count: 0
[*]results
[/LIST]

normally if there is data return it would have a count value and then a list of data but I need to validate if there is no data, i.e. if count is zero, normally, I get the data using the following code because there is a count and I would get all the data from the results list but I can't find any examples that I can get the value of count

B4X:
Dim root As Map = parser.NextObject
            Dim Count As Map = root.Get("count")
            Dim results As List = root.Get("results")
 

Colin Evans

Active Member
Licensed User
Longtime User
Sorry, I think I've found the way to do it

B4X:
    Dim root As Map = parser.NextObject
            Dim Count As String = root.Get("count")
                If Count = 0 Then
                    Msgbox("No data exists for this account","No Data")
                Else    
                Dim results As List = root.Get("results")
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
@Colin Evans have you searched the forum for JSON Tree???

B4X:
Dim parser As JSONParser
parser.Initialize("{"count":0,"next":null,"previous":null,"results":[]}")
Dim root As Map = parser.NextObject
Dim next As String = root.Get("next")
Dim previous As String = root.Get("previous")
Dim count As Int = root.Get("count")
Dim results As List = root.Get("results")

It's easy enough to modify the code to suit your needs...
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Thank you very mush, I did a search on JSON but couldn't find anything to help in identify what I need perhaps I should have been more specific in my search, thanks again, it's a lot better than my fudged attempt
 
Upvote 0
Top