iOS Question JSON Unescaped control character

Discussion in 'iOS Questions' started by aaronk, Oct 7, 2018.

  1. aaronk

    aaronk Well-Known Member Licensed User

    Hi,

    I am loading a JSON message into my B4i app but it seems to fail..

    I got the code to phrase the JSON message from here: http://www.basic4ppc.com:51042/json/index.html

    Code:
    Dim msg As String
        msg = 
    $"{"Command": "Log", "Account": "1234567",
    "events":[{"eventnum":"1","timedate":"1538867517986","eventID":"Event Message","eventype":"XYZ
    "},
    {"eventnum":"2","timedate":"1538867516716","eventID":"Event Message","eventype":"Test
    "},
    {"eventnum":"3","timedate":"1538866960068","eventID":"Event Message","eventype":"Test2
    "}]}"$

       
       
        
    Dim parser As JSONParser
        parser.Initialize(msg)
        
    Dim root As Map = parser.NextObject
        
    Dim Account As String = root.Get("Account")
        
    Dim Command As String = root.Get("Command")
        
    Dim events As List = root.Get("events")
        
    For Each colevents As Map In events
            
    Dim eventype As String = colevents.Get("eventype")
            
    Dim eventID As String = colevents.Get("eventID")
            
    Dim timedate As String = colevents.Get("timedate")
            
    Dim eventnum As String = colevents.Get("eventnum")
        
    Next
    When I run the code it shows the following error..

    It shows says the error happens on the line 176 which is the line:
    Code:
    Dim root As Map = parser.NextObject
    Code:
    Error occurred on line: 176 (EventLog)
    Error parsing 
    string: Error Domain=NSCocoaErrorDomain Code=3840 "Unescaped control character around character 135." UserInfo={NSDebugDescription=Unescaped control character around character 135.}
    Stack Trace: (
      CoreFoundation       <redacted> + 252
      libobjc.A.dylib      objc_exception_throw + 56
      CoreFoundation       <redacted> + 0
      MyApp             -[B4IJSONParser parse] + 220
      CoreFoundation       <redacted> + 144
      CoreFoundation       <redacted> + 292
      MyApp             +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1624
      MyApp             -[B4IShell runMethod:] + 448
      MyApp             -[B4IShell raiseEventImpl:method:args::] + 1640
      MyApp             -[B4IShellBI raiseEvent:event:params:] + 1560
     MyApp             +[B4IDebug delegate:::] + 80
     MyApp             -[b4i_eventlog _sortevents:] + 380
     MyApp             -[ResumableSub_eventlog_DownloadFile resume::] + 2652
     CoreFoundation       <redacted> + 144
     CoreFoundation       <redacted> + 292
     MyApp             +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1624
     MyApp             -[B4IShell runMethod:] + 448
     MyApp             -[B4IShell raiseEventImpl:method:args::] + 2164
     MyApp             -[B4IShellBI raiseEvent:event:params:] + 1560
     MyApp             -[B4IDelegatableResumableSub resume::] + 380
     MyApp             -[B4I checkAndRunWaitForEvent:event:params:] + 552
     MyApp             -[B4IShellBI raiseEvent:event:params:] + 1380
     MyApp             -[B4ICommon CallSub4::::] + 344
     MyApp             -[B4ICommon CallSub2::::] + 360
     MyApp             -[b4i_httpjob _complete:] + 188
     MyApp             -[b4i_httputils2service _completejob::::] + 412
     MyApp             -[b4i_httputils2service _hc_responsesuccess::] + 108
     CoreFoundation       <redacted> + 144
     CoreFoundation       <redacted> + 292
     MyApp             +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1624
     MyApp             -[B4I raiseEvent:event:params:] + 564
     MyApp             __61-[B4IHttp URLSession:downloadTask:didFinishDownloadingToURL:]_block_invoke + 268
     libdispatch.dylib    <redacted> + 16
     libdispatch.dylib    <redacted> + 92
     libdispatch.dylib    <redacted> + 16
     libdispatch.dylib    <redacted> + 1012
     CoreFoundation       <redacted> + 12
     CoreFoundation       <redacted> + 1964
     CoreFoundation       CFRunLoopRunSpecific + 436
     GraphicsServices     GSEventRunModal + 100
     UIKitCore            UIApplicationMain + 212
     MyApp             main + 124
     libdyld.dylib        <redacted> + 4
    )
    Any ideas on what I have done wrong ?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    json looks wrong.

    It should be:
    Code:
    msg = $"{"Command": "Log", "Account": "1234567",
    "events":[{"eventnum":"1","timedate":"1538867517986","eventID":"Event Message","eventype":"XYZ"},
    {"eventnum":"2","timedate":"1538867516716","eventID":"Event Message","eventype":"Test"},
    {"eventnum":"3","timedate":"1538866960068","eventID":"Event Message","eventype":"Test2"}
    ]}"$
     
  3. aaronk

    aaronk Well-Known Member Licensed User

    Thanks, it looks like the CRLF was causing the issue.

    I am using the same JSON with B4A and it works fine.

    I have just replaced CRLF with no data so it removes any CRLF in my JSON and it seems to fix the issue.

    Code:
    msg = msg.replace(CRLF,"")
     
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