Closed tiantianaixuexi closed 2 years ago
When the string will be modified, you should use the _strncpy
functions instead, see: API for string
For example:
yyjson_mut_obj_add_strcpy(write_doc, obj, "username", c[1].as_string().c_str());
yyjson_mut_obj_add_strncpy(write_doc, obj, "username", c[1].as_string().data(), c[1].as_string().length())
Hello, I'm trying to add some strings and integers to the for loop, but it's not working.It contains some empty strings `
for (auto& c : row) { auto obj = yyjson_mut_arr_add_obj(write_doc, write_val); yyjson_mut_obj_add_int(write_doc, obj, "id", c[0].as_int()); yyjson_mut_obj_add_str(write_doc, obj, "username", c[1].as_string().c_str()); yyjson_mut_obj_add_str(write_doc, obj, "nick", c[2].as_string().c_str()); fmt::print("{}\n", c[1].as_string().c_str()); yyjson_mut_obj_add_val(write_doc, obj, "phone", yyjson_mut_strn(write_doc, c[3].as_string().c_str(), c[3].as_string().length())); yyjson_mut_obj_add_val(write_doc, obj, "weixin", yyjson_mut_strn(write_doc, c[4].as_string().c_str(), c[4].as_string().length())); yyjson_mut_obj_add_int(write_doc, obj, "createtime", c[5].as_int()); yyjson_mut_obj_add_int(write_doc, obj, "logintime", c[6].as_int()); yyjson_mut_obj_add_val(write_doc, obj, "loginrecord", yyjson_mut_strn(write_doc, c[7].as_string().c_str(), c[7].as_string().length())); yyjson_mut_obj_add_val(write_doc, obj, "usermessage", yyjson_mut_strn(write_doc, c[8].as_string().c_str(), c[8].as_string().length())); }
`
The result of fmt::print is correct and it prints the corresponding string
4565464 sadasdsa
However, the output of 'username' Json looks like this. Why{"code":0,"data":[{"id":1,"username":"\u0000adasds","nick":"","phone":"\u000056","weixin":"","createtime":0,"logintime":0,"loginrecord":"","usermessage":""},{"id":2,"username":"\u0000adasdsa","nick":"","phone":"\u000056","weixin":"","createtime":0,"logintime":0,"loginrecord":"","usermessage":""}]}
His first 'username' should be 4565464 and his second 'username' should be 'sadasdsa' but now they are both \u0000adasds which is strange