Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从nlohmann JSON中的字符串获取JSON对象?_C++_Json_String_Nlohmann Json - Fatal编程技术网

C++ 如何从nlohmann 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

我有一个字符串,我想将其解析为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 = "[\"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++对象?