iOS Question JSON Parse issue

aaronk

Well-Known Member
Licensed User
Hi,

I have the following JSON message:

B4X:
{"Command":"GetUserServices", "NumOfServices": 2,"UserService": [{"AccountID": "ABC12345", "ServiceName": "Test 1", "ServiceStatus": "pending"},{"AccountID": "ABC98765", "ServiceName": "Test 2", "ServiceStatus": "active"}]}}

I am trying to get the values from the above JSON message in B4i.

I have used the following code.. (Got code from: http://www.basic4ppc.com:51042/json/index.html)
B4X:
Dim value As String = $"{"Command":"GetUserServices", "NumOfServices": 2,"UserService": [{"AccountID": "ABC12345", "ServiceName": "Test 1", "ServiceStatus": "pending"},{"AccountID": "ABC98765", "ServiceName": "Test 2", "ServiceStatus": "active"}]}}"$

    Dim parser As JSONParser
    parser.Initialize(value)
    Dim root As Map = parser.NextObject
    Dim NumOfServices As Int = root.Get("NumOfServices")
    Dim Command As String = root.Get("Command")
    Dim UserService As List = root.Get("UserService")
    For Each colUserService As Map In UserService
        Dim AccountID As String = colUserService.Get("AccountID")
        Dim ServiceName As String = colUserService.Get("ServiceName")
        Dim ServiceStatus As String = colUserService.Get("ServiceStatus")
        Log(ServiceName & " - " & ServiceStatus & " - " & AccountID)
    Next

When I run the above code I am getting the following error:
Application_Start
Error occurred on line: 76 (Main)
Error parsing string: Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." UserInfo={NSDebugDescription=Garbage at end.}
Stack Trace: (
CoreFoundation <redacted> + 154
libobjc.A.dylib objc_exception_throw + 38
CoreFoundation <redacted> + 0
Test App -[B4IJSONParser parse] + 212
CoreFoundation <redacted> + 68
CoreFoundation <redacted> + 294
Test App +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1784
Test App -[B4IShell runMethod:] + 574
Test App -[B4IShell raiseEventImpl:method:args::] + 1998
Test App -[B4IShellBI raiseEvent:event:params:] + 1442
Test App __33-[B4I raiseUIEvent:event:params:]_block_invoke + 74
libdispatch.dylib <redacted> + 10
libdispatch.dylib <redacted> + 22
libdispatch.dylib _dispatch_main_queue_callback_4CF + 890
CoreFoundation <redacted> + 8
CoreFoundation <redacted> + 1422
CoreFoundation CFRunLoopRunSpecific + 486
CoreFoundation CFRunLoopRunInMode + 104
GraphicsServices GSEventRunModal + 156
UIKit <redacted> + 574
UIKit UIApplicationMain + 150
Test App main + 106
libdyld.dylib <redacted> + 2
)

Line 76 is: Dim root As Map = parser.NextObject

Anyone know what I have done wrong ?

B4i Version: 3.50
Running on Windows 10 on Mac using Parallels desktop
iJSON Lib 1.00
 

Erel

Administrator
Staff member
Licensed User
There is an extra '}' character. Format it better and you will see it:
B4X:
  Dim value As String = $"{
     "Command":"GetUserServices", "NumOfServices": 2,
     "UserService": [{"AccountID": "ABC12345", "ServiceName": "Test 1", "ServiceStatus": "pending"},{"AccountID": "ABC98765", "ServiceName": "Test 2", "ServiceStatus": "active"}]
   }}"$
 
Upvote 0
Top