访问C++中的JSON值
Accessing JSON values in C++
我正试图为一个小型应用程序编写一个程序,在虚幻引擎中导航您的本地光盘。我已经使用Gradle构建了一个REST服务器,长话短说,我得到了一个带有机器目录的JSON。我想提取特定的目录名称,以字符串(特别是FText,但在这里不太重要)数组的形式返回。
我在github上找到了一个由nLohmann创建的库(https://github.com/nlohmann/json)这似乎是在c++中处理JSON的最佳方式。然而,就我的一生而言,我不知道如何提取目录名。我尝试了一个迭代器和一个简单的.value()调用。
代码和JSON示例如下,如有任何见解,将不胜感激。
char buffer[1024];
FILE *lsofFile_p = _popen("py C:\Users\jinx5\CWorkspace\sysCalls\PullRoots.py", "r");
fgets(buffer, sizeof(buffer), lsofFile_p);
_pclose(lsofFile_p);
std::string rootsJson(buffer);
string s = rootsJson.substr(1);
s = ReplaceAll(s, "'", "");
//here my string s will contain: [{"description":"Local Disk","name":"C:\"},{"description":"Local Disk","name":"D:\"},{"description":"CD Drive","name":"E:\"}]
//These are two syntax examples I found un nlohmann's docs, neither seems to work
auto j = json::parse(s);
string descr = j.value("description", "err");
我认为您的问题来自于文本字符串中的数量。
C:\
:C:\\
需要5个。
下面是一个工作示例:
#include "json.hpp"
#include <string>
using namespace std;
using json = nlohmann::json;
int main(){
json j = json::parse("[{"description":"Local Disk","name":"C:\\"},{"description":"Local Disk","name":"D:\\"},{"description":"CD Drive","name":"E:\\"}]");
cout << j.is_array() << endl;
for (auto& element : j) {
std::cout << "description : " << element["description"] << " | " << " name : " << element["name"] << 'n';
}
return 0;
}
相关文章:
- JSON转换为nlohmann JSON-lib中的结构数组
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 如何将 json 数据附加到 Qt 中的现有 json 文件中
- 可以在JSON文件的帮助下在Qt中创建UI表单
- 有没有办法从Makefile访问VS Code c_cpp_properties.json文件中的定义
- C++-JSON::值用作函数参数中的重新引用
- C++中的 Json:将数字解析为字符串以避免浮点不准确
- 使用 Json 转换器反序列化 WCF 服务中的C++字符串
- 如何正确解析 Java+C++ TCP 连接中的 JSON 对象?
- 如何使用 C++ 中的 cpprestsdk 发布 json 数据并从本地服务器获取响应
- 如何使用 boost 属性树来解析 boost 中 json 字符串中的数组中的元素?
- Visual Studio Code C++ 删除 c_cpp_properties.json 中的"定义"
- 比较 JSON::Value 变量中的数据,然后更新到文件
- 计算 JSON 中的条目数并相应地执行代码
- 如何使用Qt处理JSON中的编码西里尔字母数据
- toString():JSON 中的数字
- 如何使用rapidjson浏览.json中的数组?(椰子2d-x)
- QT:如何从服务器替换JSON中的数字(开始,结束)
- 如何使用c++解析json中的嵌套数组
- 访问nlohmann json中的元素