Parsing nested JSON - issue with string values

taneljairus

New Member
Licensed User
Longtime User
(Solved) Parsing nested JSON - issue with string values

Hi!
Just started with B4A yesterday and ran into a problem.
If JSON data is parsed in several passes, string values throw an exception at second pass, if they are empty or contain spaces (or brackets etc).
The reason for it is simple - first parsing removes all double quotes (") from the string, making it invalid for next pass.
The question - is this supposed to be like that or am I doing it wrong?

Code goes pretty much like this:
B4X:
Dim s As string
Dim the_map as map
Dim the_list as list
Dim JSON1 as JSONParser
Dim JSON2 as JSONParser

s = "JSON data"
JSON1.initialize (s)
the_map = JSON1.nextobject
s = the_map.get("Keyword")
JSON2.initialize (s)
the_list = JSON2.nextarray

Now, this is by no means a showstopper and I can work around it. It just would be nice if I didn't have to.
 
Last edited:

taneljairus

New Member
Licensed User
Longtime User
Replaced actual values with placeholders, but here it is.
B4X:
{
    "displayFieldName": "sign_code",
    "fieldAliases": {
        "sign_id": "sign_id",
        "label": "label"
    },
    "geometryType": "esriGeometryPoint",
    "spatialReference": {
        "wkid": 4326
    },
    "fields": [
        {
            "name": "sign_id",
            "type": "esriFieldTypeInteger",
            "alias": "sign_id"
        },
        {
            "name": "label",
            "type": "esriFieldTypeString",
            "alias": "label"
        }
    ],
    "features": [
        {
            "attributes": {
                "sign_id": 29,
                "label": "first_part second_part"
            },
            "geometry": {
                "x": 1000,
                "y": 1000
            }
        }
    ]
}
 
Upvote 0

taneljairus

New Member
Licensed User
Longtime User
"Keyword" was meant as placeholder. For actual code, it should be "features".

But yes, second parsing really is unnecessary. After learning a bit about collections even I understood that.
And my question in first post was answered - it is really supposed to work that way and I was doing it wrong.
 
Upvote 0

Felix Maria

Member
Licensed User
Longtime User
Hi,

I have a stored procedure that returns about 3 set of results. It is serialised and send back with the use of web services.
The following is the sample of the json string

(ArrayList) [{Value=[{REST_NAME=Lounge, TBLNAM=RESCOD, CODE=001}], Key=RESCOD},
{Value=[{SESSION_NAME=General, TBLNAM=SESSON, FROM_TIME=0.00}], Key=SESSON},
{Value=[{DESCRP=General, TBLNAM=RATSLB, RATSLB=1},
{DESCRP=Happy Hours, TBLNAM=RATSLB, RATSLB=2}], Key=RATSLB}]

The above data contains 3 arrays of values.
I need to split this string data and store it in to the respective global arrays in the program.
How do I do It?

I tried doing it the following way...But it throws and null exception error....

response = HttpUtils.GetString(SPURL)
parser.Initialize(response)
rows = parser.NextArray
Do While rows.Size <> 0
Dim Tbl As Map
Tbl = rows.Get(0)
Select Case Tbl.Get("TBLNAM")
Case "RESCOD"
GnResArray.Initialize
Case "SESSON"
GnResArray.Initialize
Case "RATSLB"
GnResArray.Initialize
End Select

Please guide me on the usage of nested json parsing. When I tried to use the NextObject.....it again gave me an error...

Thank You.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…