parse a json file
-
Hello,
I am trying to parse a json file in which I have data like me urls to my server.
For the parser I am using a library called nlohmann_json.
Now my problem is when I am trying to use the library to parse and use my config.json I am getting the following error:
E libc++abi: terminating with uncaught exception of type nlohmann::json_abi_v3_11_3::detail::parse_error: [json.exception.parse_error.101] parse error at line 1, column 1: attempting to parse an empty input; check that your input string or stream contains the expected JSON
This seems to be expected since I am not sure if I have passed my config.json to my apk build as I should have.
As far as I am aware this should be done with qt_add_resources, but still I had no success in parsing my file.
Here's the part of my CMakeLists.txt that I think is important
set(PROJECT_SOURCES src/main.cpp src/AndroidService.cpp src/NetworkManager.cpp src/StorageManager.cpp src/NotificationManager.cpp src/utility/permission.cpp src/utility/device.cpp src/services/BootService.cpp src/services/LocationService.cpp src/services/CameraService.cpp src/services/TrackLocationService.cpp src/config.json src/Config.cpp src/json.hpp ) qt_add_executable(secureme MANUAL_FINALIZATION ${PROJECT_SOURCES} ) set(JSON_Install OFF CACHE INTERNAL "") add_subdirectory(src/nlohmann_json) target_link_libraries(secureme PRIVATE Qt6::Gui Qt6::CorePrivate Qt6::Network Qt6::Positioning Qt6::Widgets Qt6::Multimedia Qt6::WebSockets Qt6::Concurrent Qt6::Qml nlohmann_json::nlohmann_json ) if(ANDROID) set(ANDROID_TARGET_SDK_VERSION 34) add_android_openssl_libraries(secureme) set_property(TARGET secureme APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/android) qt6_add_resources(secureme "main" PREFIX "/" FILES "qml/CustomTextField.qml" "qml/Auth.qml" "qml/AppStyle.qml" "qml/CustomButton.qml" "qml/home.qml" "qml/main.qml" "qml/images/logo.png" "src/config.json" ) qt_finalize_executable(secureme) endif()
as you can see I have both added it to my PROJECT_SOURCES and qt resources since I wasn't sure where to put it.
Could someone help me try to figure out why do I get such an error? Where I have messed up?
-
Hi,
How are you passing that file to nlohmann's library ?
If you try to pass it the path to the QRC file, then it won't work as non Qt libraries don't know how to access such resources.
You need to either copy the file to a temporary path or load it to a buffer and pass that further.Note that Qt also has JSON support.