Android Question Problem decoding JSON with array

DKnowles

Member
Licensed User
Longtime User
Hi All

Having a bit of a mare with decoding a json dataset

Example:
Dim json As JSONParser
Dim text As String '= j.GetString
                
text = $"[{            "errors": "OK,203",
                    "LegOtherID": "100321888",
                    "ExtraTypeID": "19233",
                    "Reading": "Test Data",
                    "SecurityID": "10801",
                    "ReadingType": "108",
                    "base64": "Test Data",
                    "decimal": "Test Data",
                    "integer": "Test Data",
                    "boolean": "Test Data",
                    "Text": "Test Data"
                }, {
                    "errors": "OK,203",
                    "LegOtherID": "100321888",
                    "ExtraTypeID": "19235",
                    "Reading": "Test Data",
                    "SecurityID": "10801",
                    "ReadingType": "108",
                    "base64": "Test Data",
                    "decimal": "Test Data",
                    "integer": "Test Data",
                    "boolean": "Test Data",
                    "Text": "Test Data"
                }, {
                    "errors": "OK,203",
                    "LegOtherID": "100321888",
                    "ExtraTypeID": "19237",
                    "Reading": "Test Data",
                    "SecurityID": "10801",
                    "ReadingType": "108",
                    "base64": "Test Data",
                    "decimal": "Test Data",
                    "integer": "Test Data",
                    "boolean": "Test Data",
                    "Text": "Test Data"
                }]"$
                
json.Initialize(text)
Dim m As Map = json.NextObject

When I execute this get 'java.lang.RuntimeException: JSON Object expected.' but the JSON is valid and all the validators I could find

Thanks in advance David
 

drgottjr

Expert
Licensed User
Longtime User
the json may be valid, but your reading of it isn't. it's NextArray, not NextObject. all the objects are held in the array.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
1. http://basic4ppc.com:51042/json/index.html
2. This is the correct code
B4X:
Dim text As String = $"[{            "errors": "OK,203",
                    "LegOtherID": "100321888",
                    "ExtraTypeID": "19233",
                    "Reading": "Test Data",
                    "SecurityID": "10801",
                    "ReadingType": "108",
                    "base64": "Test Data",
                    "decimal": "Test Data",
                    "integer": "Test Data",
                    "boolean": "Test Data",
                    "Text": "Test Data"
                }, {
                    "errors": "OK,203",
                    "LegOtherID": "100321888",
                    "ExtraTypeID": "19235",
                    "Reading": "Test Data",
                    "SecurityID": "10801",
                    "ReadingType": "108",
                    "base64": "Test Data",
                    "decimal": "Test Data",
                    "integer": "Test Data",
                    "boolean": "Test Data",
                    "Text": "Test Data"
                }, {
                    "errors": "OK,203",
                    "LegOtherID": "100321888",
                    "ExtraTypeID": "19237",
                    "Reading": "Test Data",
                    "SecurityID": "10801",
                    "ReadingType": "108",
                    "base64": "Test Data",
                    "decimal": "Test Data",
                    "integer": "Test Data",
                    "boolean": "Test Data",
                    "Text": "Test Data"
                }]"$

Dim parser As JSONParser
parser.Initialize(text)
Dim root As List = parser.NextArray
For Each colroot As Map In root
 Dim Reading As String = colroot.Get("Reading")
 Dim LegOtherID As String = colroot.Get("LegOtherID")
 Dim ReadingType As String = colroot.Get("ReadingType")
 Dim boolean As String = colroot.Get("boolean")
 Dim SecurityID As String = colroot.Get("SecurityID")
 Dim base64 As String = colroot.Get("base64")
 Dim Text As String = colroot.Get("Text")
 Dim integer As String = colroot.Get("integer")
 Dim decimal As String = colroot.Get("decimal")
 Dim errors As String = colroot.Get("errors")
 Dim ExtraTypeID As String = colroot.Get("ExtraTypeID")
Next

The json contains a LIST of Maps but you are trying to get a Map directly.
 
Last edited:
Upvote 1
Top