iOS Question Parse json failing

IamTrying

Active Member
Licensed User
Failing to parse json string from a file. "jMap = JSON.NextObject".

B4X:
Application_Start
>>> Login file exists
Error occurred on line: 226 (Main)
Error parsing string: Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around character 1." UserInfo={NSDebugDescription=No string key for value in object around character 1.}
Stack Trace: (
  CoreFoundation       <redacted> + 148
  libobjc.A.dylib      objc_exception_throw + 56
  CoreFoundation       <redacted> + 0
  B4i Pushclient       -[B4IJSONParser parse] + 220
  CoreFoundation       <redacted> + 144
  CoreFoundation       <redacted> + 292
  B4i Pushclient       +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1624
  B4i Pushclient       -[B4IShell runMethod:] + 448
  B4i Pushclient       -[B4IShell raiseEventImpl:method:args::] + 1260
  B4i Pushclient       -[B4IShellBI raiseEvent:event:params:] + 1408
 B4i Pushclient       __33-[B4I raiseUIEvent:event:params:]_block_invoke + 60
 libdispatch.dylib    <redacted> + 24
 libdispatch.dylib    <redacted> + 16
 libdispatch.dylib    <redacted> + 1016
 CoreFoundation       <redacted> + 12
 CoreFoundation       <redacted> + 2012
 CoreFoundation       CFRunLoopRunSpecific + 436
 GraphicsServices     GSEventRunModal + 100
 UIKit                UIApplicationMain + 236
 B4i Pushclient       main + 124
 libdyld.dylib        <redacted> + 4
)
<B4IExceptionWrapper: Error Domain=caught_exception Code=0 " Error parsing string: Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around character 1." UserInfo={NSDebugDescription=No string key for value in object around character 1.}" UserInfo={NSLocalizedDescription= Error parsing string: Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around character 1." UserInfo={NSDebugDescription=No string key for value in object around character 1.}}>

B4X:
Sub JsonToMap(strJSON As String) As Map
    ' convert a json string to a map
    Dim jMap As Map
    jMap.Initialize
    Dim JSON As JSONParser
    JSON.Initialize(strJSON)
    jMap = JSON.NextObject '<<<< failing
    Return jMap
End Sub

'read
jsonLine = File.ReadString(File.DirDocuments, "file.dat")
        'parser.Initialize(jsonLine)
        JsonToMap(jsonLine)

'written as:
Dim jsonLine As String
                    jsonLine = $"{a:""$ & a& _
                    $"",b:""$ & myUsername & _
                    $"",c:""$ & myPassword & _
                    $"",d:""$ & c & _
                    $"",e:""$ & b & $""}"$
 
Last edited:

IamTrying

Active Member
Licensed User
Sample code of fail while using iJSON version 1.00

B4X:
'Code module
#Region  Project Attributes
    #ApplicationLabel: B4i Json
    #Version: 1.0.0
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
    #Target: iPhone, iPad
    #ATSEnabled: True
    #MinVersion: 7
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page

End Sub

Sub JsonToMap(strJSON As String) As Map
    ' convert a json string to a map
    Dim jMap As Map
    jMap.Initialize
    Dim JSON As JSONParser
    JSON.Initialize(strJSON)
    jMap = JSON.NextObject '<<<< failing
    Return jMap
  
End Sub


Private Sub Application_Start (Nav As NavigationController)
    'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    NavControl.ShowPage(Page1)
  
    Dim jsonLine As String
    jsonLine = $"{a:"a",b:"b",c:"c",d:"d",e:"e"}"$  
    Log(jsonLine)
          
    JsonToMap(jsonLine)
  
End Sub

Private Sub Page1_Resize(Width As Int, Height As Int)
  
End Sub

Private Sub Application_Background
  
End Sub

EDIT: if the string is jsonLine = $"{"a":"a"}"$ then it works
 
Upvote 0
Top