我在 JSONcpp 中有一个根,具有这样的字符串值。
Json::Value root;
std::string val = "{\"stringval\": \"mystring\"}";
Json::Reader reader;
bool parsingpassed = reader.parse(val, root, false);
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试使用这段代码检索该值时。
Json::StreamWriterBuilder builder;
builder.settings_["indentation"] = "";
std::string out = Json::writeString(builder, root["stringval"]);
Run Code Online (Sandbox Code Playgroud)
理想情况下,这里的字符串应该包含:
"mystring"
Run Code Online (Sandbox Code Playgroud)
而它给出的输出是这样的:
"\"mystring\"" \\you see this in debug mode if you check your string content
Run Code Online (Sandbox Code Playgroud)
顺便说一下,如果你使用stdout它打印这个值,将会打印如下内容:
"mystring" \\ because \" is an escape sequence and prints " in stdout
Run Code Online (Sandbox Code Playgroud)
它应该像这样打印stdout:
mystring \\Expected output
Run Code Online (Sandbox Code Playgroud)
知道如何在将 json 输出转换为 std::string 时避免这种输出吗?请避免建议使用 fastwriter,因为它还添加了换行符并且也弃用了 API。
约束:我不想通过字符串操作删除额外的 \" 来修改字符串,而是愿意知道如何直接使用 JSONcpp 来做到这一点。
我在格式化包含引号的字符串时遇到问题.
例如,我得到了这个std :: string: server/register?json={"id"="monkey"}
此字符串需要将四个引号替换为\",因为它将用作另一个函数的c_str().
如何在这个字符串上做到最好?
{"id"="monkey"}
Run Code Online (Sandbox Code Playgroud)
编辑:我需要一个只使用STL库的解决方案,最好只使用String.h.我已经确认我需要替换"with".
EDIT2:Nvm,在框架中发现了这个bug
{
"80550560": {"name":" HAdailton Cesar", "name2": "T-Max"},
"5987810": {"name": "Adnax", "name2": "Adna Zaza"}
}
Run Code Online (Sandbox Code Playgroud)
我有这个输入,我需要输出输入中的所有名称,但问题是我没有整数有组织的索引,我将不得不得到字符串数字,我也不知道字符串是什么文本索引将是.
我会想象这样的事情,但我不知道如何从JsonCPP获取'string_text'
res[string_text]["name"];
Run Code Online (Sandbox Code Playgroud) 昨天,我在团队的代码中使用 jsoncpp 库遇到了这样的函数:
#include <json/value.h>
Json::Value makeList() {
Json::Value list(Json::arrayValue);
list.append(new Json::Value(true));
return list;
}
Run Code Online (Sandbox Code Playgroud)
它引起了我的注意,因为new在调用中使用list.append泄漏了一些内存,这可以通过简单地删除来修复new。然而,在调试过程中,我意识到Json::Value::append需要aconst Json::Value&或aJson::Value&&。C++ 不会将指针隐式转换为引用,因此 aJson::Value*不应隐式转换为其中任何一个。
为了证实这一点,我写了自己的简短示例:
#include <iostream>
class Foo {
public:
int x = 4;
};
void printValue(const Foo& f) { std::cout << "The value is " << f.x << ".\n"; }
void printValue(Foo&& f) { std::cout << "The value is " << f.x << ".\n"; } …Run Code Online (Sandbox Code Playgroud) 在 JSON 中序列化 utf-8 字符串的标准方法是什么?它应该带有 u 转义序列还是应该是十六进制代码。
\n\n我想用 JSON 格式的单位序列化一些传感器读数。
\n\n例如,我的温度读数单位为 \xc2\xb0C。是否应该序列化为
\n\n{\n "units": "\\u00b0"\n}\n\xc2\xb4\xc2\xb4\xc2\xb4\nor should it be something like \n\xc2\xb4\xc2\xb4\xc2\xb4\n{\n "units":"c2b0"\n}\nRun Code Online (Sandbox Code Playgroud)\n\n或者这两者都可以得到标准支持。
\n我正在用 C++ 制作一个文本冒险,作为学习该语言的尝试。玩家使用“看”或“向北行进”等命令进行交互。到目前为止,我已经得到了将文本转换为小写然后将其拆分为向量的代码。这对于单字命令来说效果很好,但是当我去实现更长的命令时,我发现长度总是 = 1 并且访问它会返回错误。这是我的代码:
//Input function.
void Input() {
const char delim = ' ';
//Processing
std::cin >> input;
std::transform(input.begin(), input.end(), input.begin(),
[](unsigned char c) { return std::tolower(c); });
std::vector<std::string> out;
split(input, ' ', out);
//Commands
if (out[0] == "exit")
exit(0);
else if (out[0] == "help")
Help();
else if (out[0] == "look")
Look();
else if (out[0] == "travel" || out[0] == "go") {
//This code will never run.
if (int(out.size()) == 2) {
Travel(out[1]); …Run Code Online (Sandbox Code Playgroud) make()我在类中有一个静态方法Response。它被重载以从 jsoncpp 获取std::string或Json::Value获取。
我重载了该方法,如下所示:
Response::make(Json::Value body)
{
...
}
Response::make(std::string body)
{
...
}
Run Code Online (Sandbox Code Playgroud)
这会导致编译时出现此错误:
Response::make(Json::Value body)
{
...
}
Response::make(std::string body)
{
...
}
Run Code Online (Sandbox Code Playgroud)
是否被Json::value视为字符串?我该如何解决这个问题?