iOS Question JSONParser issue

alex2103

Member
Licensed User
Longtime User
In the b4a and b4j application, I use JSONParser to parse invalid JSON strings.
For example,
“[cur-version, 27]”
These lines I get through the network through AsyncStreams.

In b4a and b4j it works fine
B4X:
Private parser As JSONParser
parser.Initialize (text)
Private root As List
root = parser.NextArray
and I get a list with elements

In b4i, I get the Error parsing string because the strings are not in quotes.
error: Error Domain = NSCocoaErrorDomain Code = 3840 "Invalid value around character 1." UserInfo = {NSDebugDescription = Invalid value around character 1.}

Unfortunately, I do not have access to the server side code...
 

alex2103

Member
Licensed User
Longtime User
other strings are not so simple. "[cur-version, 27]" is only for simple examle.
Main part of the protocol is the list-of-lists in JSON format. More messages longer than 4000 Log limit...
In fact, this is like the s-exp format of the LISP language, which is almost similar to json.

Examle from b4a:
B4X:
Log($"sexp=${text}"$)
text = toJSON(text)
Log($"JSON=${text}"$)
Dim parser As JSONParser
parser.Initialize(text)
Dim root As List = parser.NextArray
Log($"ProcessCommand=${root}"$)

sexp=(gps-data2 19504 (34 0 17 16 4 2019) (0 47.78644333333333d0 35.182453333333335d0) 0)
JSON=[gps-data2,19504,[34,0,17,16,4,2019],[0,47.78644333333333d0,35.182453333333335d0],0]
ProcessCommand=(ArrayList) [gps-data2, 19504, [34, 0, 17, 16, 4, 2019], [0, 47.78644333333333d0, 35.182453333333335d0], 0]

sexp=(gps-data2 54906 (23 4 17 16 4 2019) (0 47.30813333333333d0 31.777095d0) 1576)
JSON=[gps-data2,54906,[23,4,17,16,4,2019],[0,47.30813333333333d0,31.777095d0],1576]
ProcessCommand=(ArrayList) [gps-data2, 54906, [23, 4, 17, 16, 4, 2019], [0, 47.30813333333333d0, 31.777095d0], 1576]
In Logs the string is very similar to the string representation of the object, but I don’t know how to make an List object from this string.
In b4a very large lines of this format are handled well with JsonParser.

PS: sorry for my English and google translate :)
 
Upvote 0

alex2103

Member
Licensed User
Longtime User
You have two options:
1. Modify the json string to make it valid.
2. Parse it yourself.

Only you can know which option is simpler. If for example the strings all look like gps-data2 then a simple replace of this term will make it a valid json string.

I think I will go along the first path and will modify the json string to the correct format.
For b4a, the application has long been completely ready and I would like to use one code in b4i b4a and b4j.
Maybe you can change the iJSON library (version 1.00) so that it works like b4a JSON (version 1.10)?
 
Upvote 0
Top