iOS Question JSON Unescaped control character

aaronk

Well-Known Member
Licensed User
Longtime 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.b4x.com:51042/json/index.html

B4X:
    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:
B4X:
Dim root As Map = parser.NextObject

B4X:
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 ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
json looks wrong.

It should be:
B4X:
 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"}
]}"$
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
json looks wrong.
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.

B4X:
msg = msg.replace(CRLF,"")
 
Upvote 0
Top