pbjsonlib/jsonlib.pb
2024-11-09 20:57:45 -06:00

64 lines
No EOL
1.9 KiB
Text

UseSQLiteDatabase()
; worry about the database init later
OpenDatabase(0,"json.db","","",#PB_Database_SQLite)
LoadJSON(1,"results.json");ParseJSON(1, json$) ; returns garbage without the PB ascii there not sure why
ParseJSON(1, json$) ; returns garbage without the PB ascii there not sure why
ObjectValue = JSONValue(1)
If ExamineJSONMembers(ObjectValue)
While NextJSONMember(ObjectValue)
key$ = JSONMemberKey(ObjectValue)
MemberValue = JSONMemberValue(ObjectValue)
parentKey$ = "" ; Assuming no parent key information is available in the current code.
; Now extract the actual value based on its type
Select JSONType(MemberValue)
Case #PB_JSON_String
value$ = GetJSONString(MemberValue)
type$ = "String"
Case #PB_JSON_Number
value$ = StrD(GetJSONDouble(MemberValue))
type$ = "Number"
Case #PB_JSON_Boolean
value$ = Str(Bool(GetJSONBoolean(MemberValue))) ;Feels cheaty, but it workssss
type$ = "Boolean"
Case #PB_JSON_Object
value$ = "(nested object)"
;Debug key$
parentKey$ = key$
type$ = "object"
; You could further examine this nested object if needed.
Case #PB_JSON_Array
value$ = "(nested array)"
type$ = "Array"
; Handle array values if needed.
Default
value$ = "(unknown type)"
type$ = "Unknown"
EndSelect
; Insert the parsed data into the SQLite database
query$ = "INSERT INTO json_data (key, parent_key, type, value) VALUES ('" + key$ + "', '" + parentKey$ + "', '" + type$ + "', '" + value$ + "')"
If DatabaseUpdate(0, query$) = 0
Debug "Error inserting data: " + DatabaseError()
EndIf
Wend
EndIf
; IDE Options = PureBasic 6.12 LTS (Linux - x64)
; CursorPosition = 37
; FirstLine = 15
; EnableXP
; DPIAware