Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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++ 二进制数据JSONCPP_C++_Json_Visual C++_Jsoncpp - Fatal编程技术网

C++ 二进制数据JSONCPP

C++ 二进制数据JSONCPP,c++,json,visual-c++,jsoncpp,C++,Json,Visual C++,Jsoncpp,我正在尝试在VS2008中使用JSON cpp 有人能告诉我是否可以将二进制数据打包成JSON格式?我正在将图像文件读入char*buffer,并将其放入JSON::Value。但是当我试图解析它时,在JSON对象中找不到缓冲区内容 代码如下 Json::Value root; Json::Reader reader; Json::StyledWriter writer; int length; char * buffer; ifstream is

我正在尝试在VS2008中使用JSON cpp

有人能告诉我是否可以将二进制数据打包成JSON格式?我正在将图像文件读入
char*buffer
,并将其放入
JSON::Value
。但是当我试图解析它时,在JSON对象中找不到缓冲区内容

代码如下

    Json::Value root;
    Json::Reader reader;
    Json::StyledWriter writer;
    int length;
    char * buffer;
    ifstream is;
    is.open ("D:\\test.j2k", ios::binary);

    // get length of file:
    is.seekg (0, ios::end);
    length = is.tellg();
    is.seekg (0, ios::beg);

    // allocate memory:
    buffer = new char [length];

    // read data as a block:
    is.read (buffer,length);
    root["sample"] = *buffer;
    writer.write(root);  
    cout << root;
    const string rootAsString  = root.toStyledString();
    cout << rootAsString << endl;
Json::Value root;
Json::阅读器;
Json::StyledWriter编写器;
整数长度;
字符*缓冲区;
如果流是;
is.open(“D:\\test.j2k”,ios::binary);
//获取文件的长度:
is.seekg(0,ios::end);
长度=is.tellg();
is.seekg(0,ios::beg);
//分配内存:
缓冲区=新字符[长度];
//将数据作为块读取:
is.read(缓冲区、长度);
根[“样本”]=*缓冲区;
writer.write(root);

cout您必须对其进行编码,因为JSON是javascript源代码中显示的javascript结构格式的子集

JSON中最常用的二进制数据编码是Base64。我使用它(在c++以外的其他语言中)对图像进行编码,没有任何问题。只需在编码图像前面加上
data:image/png;base64,
(假设它是png),如果您将其设置为图像的src,它将在javascript中自动解码


<>编辑:与其他任何语言一样,C++中的Base64编码也很容易。这里有一个库:

你能用上面的代码解释一下吗?我在哪里犯了错误?您将文件的内容放入您的值中,并要求StyledWriter对其进行编码。它怎么知道它应该添加我建议的前缀并在base64中编码?