[SOLVED]How to write json Array using rapidjson?

I want to write json array as:- [4,50,0,52,14,15,17,21,41]. Below is my code .It gives me crash. what I am doing wrong?

std::vector<int> USER_VAL = {4,50,0,52,14,15,17,21,41};

rapidjson::Document document;
document.SetArray();
rapidjson::Value array(rapidjson::kArrayType);

rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
for (int i = 0; i < USER_VAL.size(); i++)
{
    array.PushBack(USER_VAL.at(i),allocator);
}
document.AddMember("",array, allocator);
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
document.Accept(writer);

log("JsonData1 = %s", strbuf.GetString());

Thank you!!

Try

std::vector<int> USER_VAL = {4,50,0,52,14,15,17,21,41};

rapidjson::Document document;
document.SetArray();

rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
for (int i = 0; i < USER_VAL.size(); i++)
{
    document.PushBack(USER_VAL.at(i),allocator);
}
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
document.Accept(writer);

log("JsonData1 = %s", strbuf.GetString());
1 Like

Thank you !! its working !!

I get this error on 3.13 No type named 'StringBuffer' in namespace 'rapidjson'

You need to include “stringbuffer.h”

I want to add std::vector<std::vector> _arr, will it work…?

iterate through your std::vector in a loop and do what you need to form proper JSON? It’s the same concepts as array