iOS Question Parse json failing

Discussion in 'iOS Questions' started by IamTrying, Dec 18, 2017.

  1. IamTrying

    IamTrying Active Member Licensed User

    Failing to parse json string from a file. "jMap = JSON.NextObject".

    Code:
    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.}}>
    Code:
    Sub JsonToMap(strJSON As StringAs 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: Dec 18, 2017
  2. IamTrying

    IamTrying Active Member Licensed User

    Sample code of fail while using iJSON version 1.00

    Code:
    '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 StringAs 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
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Why are you creating the json string like this???

    You can check your strings with this tool: https://jsonlint.com/
    They are not valid.

    Correct way:
    Code:
    Dim jg As JSONGenerator
    jg.Initialize(CreateMap(
    "a": a, "b": myUserName, _
      
    "c": myPassword))
    Dim jsonstring As String = jg.ToString
     
    IamTrying likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice