iOS Question JSON Parser

David Hawkins

Active Member
Licensed User
Longtime User
Hi I am currently modifying an Android App to work on IOS and I am retrieving data from a database and parsing it using the following code

Parsing:
            Dim parser As JSONParser
            Dim response As String = job.GetString
            Log(response)
            parser.Initialize(response)
            
            Dim R As Map = parser.NextObject

            For Each colroot As Map In R
                Main.Usertype = colroot.Get("Usertype")
                Main.UserID = colroot.Get("ID")
                Main.UserCompanyName = colroot.Get("CompanyName")
                Main.CompanyName = colroot.Get("CompanyName")
                Main.UserCompanyType = colroot.Get("CompanyType")
            Next

I have tried Dim R As Map = parser.NextArray as well but both fail with the following

Error parsing string: Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end around line 1, column 68." UserInfo={NSDebugDescription=Garbage at end around line 1, column 68., NSJSONSerializationErrorIndex=68}

The code in B4A works perfectly therefore I am not seeing where it is going wrong as the JSON string I get back from the database is what I would expect

Below is the JSON string
[{"ID":4,"Usertype":"Admin","UserCompanyName":"A R Stacey and Son"}]<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><CheckCredentialsResponse xmlns="http://tempuri.org/" />

Could somebody tell me where I am going wrong?

Regards
David
 

mcqueccu

Well-Known Member
Licensed User
Longtime User
Works fine. Use this https://b4x.com:51041/json/index.html


B4X:
    Dim json As String = $"[{"ID":4,"Usertype":"Admin","UserCompanyName":"A R Stacey and Son"}]"$
    
    Dim parser As JSONParser
    parser.Initialize(json)
    Dim jRoot As List = parser.NextArray
    For Each coljRoot As Map In jRoot
        Dim Usertype As String = coljRoot.Get("Usertype")
        Dim ID As Int = coljRoot.Get("ID")
        Dim UserCompanyName As String = coljRoot.Get("UserCompanyName")
    Next
    
    Log(Usertype)
    Log(ID)
    Log(UserCompanyName)
 
Upvote 0
Top