简体   繁体   English

将未签名的char *转换为json,jsonspp C ++

[英]convert unsigned char * to json, jsonspp C++

I have unsigned char array holding data, I want to store this into a Json object. 我有保存数据的无符号字符数组,我想将其存储到一个Json对象中。 How can it be done? 如何做呢? Using jsoncpp and C++11. 使用jsoncpp和C ++ 11。 the statement 该声明

rootL1["CurrentValue"] = userInfo->hashCred;

does not work, in turn it is stored as bool in json instead of the unsigned data. 不起作用,依次将其作为布尔存储在json中,而不是未签名的数据中。

If you can look into documentation of jsoncpp Value class (I assume rootL1 is of this type, and operator[] returns the same type), you will see that there is no unsigned char* conversion constructor which would accept your userInfo->hashCred array. 如果您可以查看jsoncpp Value类的文档(我假设rootL1属于这种类型,并且operator[]返回相同的类型),您将看到没有可以接受userInfo->hashCred数组的unsigned char*转换构造函数。 。 There is one constructor which accepts const char* but there is no implicit conversion from unsigned char* to const char* , the closest one is implicit conversion of pointer to bool which is choosen in your case. 有一个接受const char*构造函数,但是没有从unsigned char*const char*的隐式转换,最接近的一个是在您的情况下选择的bool指针的隐式转换。

The solution should be to use conversion constructor which accepts const char* . 解决方案应该是使用接受const char*转换构造const char* You should think why you need unsigned char* in the first place, maybe you can use an array of type char ? 您应该考虑一下为什么首先需要unsigned char* ,也许您可​​以使用char类型的数组? If you cant then you can try casting unsigned char* to char* : 如果不能,则可以尝试将unsigned char*强制转换为char*

rootL1["CurrentValue"] = reinterpret_cast<char *>(userInfo->hashCred)

but this can cause all sorts of problems depending on what you are actually storing in your array and also what jsoncpp is doing under the hood with this data. 但这可能会导致各种问题,具体取决于您实际存储在数组中的内容以及jsoncpp使用此数据进行的操作。

I would suggest you treat your hashCred as binary data and uuencode it to char array. 我建议您将hashCred视为二进制数据,并将其uuencode编码为char数组。 Then store it in jsoncpp Value. 然后将其存储在jsoncpp Value中。 For details look into here: Binary data JSONCPP 有关详细信息,请查看此处: 二进制数据JSONCPP

In jsoncpp test code you can also find some examples of how to store binary data as values. jsoncpp测试代码中,您还可以找到一些有关如何将二进制数据存储为值的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM