C++ 如何从nlohmann JSON中的字符串获取JSON对象?
我有一个字符串,我想将其解析为json,但是C++ 如何从nlohmann JSON中的字符串获取JSON对象?,c++,json,string,nlohmann-json,C++,Json,String,Nlohmann Json,我有一个字符串,我想将其解析为json,但是\u json似乎不是每次都有效 #include <nlohmann/json.hpp> #include <iostream> using nlohmann::json; int main() { // Works as expected json first = "[\"nlohmann\", \"json\"]"_json; // Doesn't work std::string s
\u json
似乎不是每次都有效
#include <nlohmann/json.hpp>
#include <iostream>
using nlohmann::json;
int main()
{
// Works as expected
json first = "[\"nlohmann\", \"json\"]"_json;
// Doesn't work
std::string s = "[\"nlohmann\", \"json\"]"_json;
json second = s;
}
#包括
#包括
使用nlohmann::json;
int main()
{
//工作如期进行
json first=“[\“nlohmann\”,\“json\”]”\u json;
//不起作用
std::string s=“[\“nlohmann\”,\“json\”]”\u json;
json秒=s;
}
第一部分工作,第二部分在抛出“nlohmann::detail::type_error”的实例后抛出terminate调用
what()
显然,JSON对象可以等于JSON值,但字符串不能
在这种情况下,您必须从文本中删除\u json
,但这会使second
成为隐藏在json对象中的字符串值
因此,您还可以使用json::parse
,如下所示:
std::string s = "[\"nlohmann\", \"json\"]";
json second = json::parse(s);
是否尝试将JSON直接解析为C++对象?